Changeset 7839f78


Ignore:
Timestamp:
Aug 10, 2023, 10:18:55 PM (9 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
13600e8
Parents:
9ff71ff
Message:

update documentation about changes to I/O

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/user/user.tex

    r9ff71ff r7839f78  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Thu Jun 29 21:52:51 2023
    14 %% Update Count     : 5533
     13%% Last Modified On : Thu Aug 10 21:24:07 2023
     14%% Update Count     : 5570
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    37213721\end{cquote}
    37223722The \CFA form has half the characters of the \CC form, and is similar to \Index*{Python} I/O with respect to implicit separators and newline.
    3723 Similar simplification occurs for \Index{tuple} I/O, which flattens the tuple and prints each value separated by ``\lstinline[showspaces=true]{, }''.
     3723Similar simplification occurs for \Index{tuple} I/O, which flattens the tuple and prints each value separated by ``\lstinline[showspaces=true]{, }'' (comma space).
    37243724\begin{cfa}
    37253725[int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ];
     
    37513751\end{tabular}
    37523752\end{cquote}
    3753 Input and output use a uniform operator, ©|©, rather than \CC's ©>>© and ©<<© input/output operators.
    37543753There is a weak similarity between the \CFA logical-or operator and the \Index{Shell pipe-operator} for moving data, where data flows in the correct direction for input but the opposite direction for output.
     3754Input and output use a uniform operator, ©|©, rather than \CC's ©<<© and ©>>© input/output operators, which prevents this common error in \CC:
     3755\begin{C++}
     3756cin << i; // why is this generating a lot of error messages?
     3757\end{C++}
    37553758
    37563759For implicit formatted input, the common case is reading a sequence of values separated by whitespace, where the type of an input constant must match with the type of the input variable.
     
    38553858\Indexc{close} flushes the stream and closes the file.
    38563859\item
    3857 \Indexc{write} (©ofstream© only) write ©size© bytes to the stream.
    3858 The bytes are written lazily to file when internal buffers fill.
     3860\Indexc{write} (©ofstream© only) writes ©size© bytes to the stream.
     3861The bytes are written lazily when an internal buffer fills.
    38593862Eager buffer writes are done with ©flush©
    38603863\item
    3861 \Indexc{read} (©ifstream© only) read ©size© bytes to the stream.
     3864\Indexc{read} (©ifstream© only) reads ©size© bytes from the stream.
    38623865\item
    38633866\Indexc{ungetc} (©ifstream© only) pushes the character back to the input stream.
     
    39683971
    39693972\item
    3970 If a space is desired before or after one of the special string start/end characters, simply insert a space.
     3973If a space is desired before or after one of the special string start/end characters, explicitly insert a space.
    39713974\begin{cfa}
    39723975sout | "x ($\R{\texttt{\textvisiblespace}}$" | 1 | "$\R{\texttt{\textvisiblespace}}$) x" | 2 | "$\R{\texttt{\textvisiblespace}}$, x" | 3 | "$\R{\texttt{\textvisiblespace}}$:x:$\R{\texttt{\textvisiblespace}}$" | 4;
     
    407340761, 2, 34, 5, 6
    40744077\end{cfa}
    4075 ©sep© \emph{cannot} be used to start/end a line with a separator because separators do not appear at the start/end of a line;
    4076 use ©sep© to accomplish this functionality.
     4078©sep© \emph{cannot} be used to start/end a line with a separator because separators do not appear at the start/end of a line.
     4079Use ©sep© to accomplish this functionality.
    40774080\begin{cfa}[belowskip=0pt]
    40784081sout | ®sep® | 1 | 2 | 3 | ®sep®; $\C{// sep does nothing at start/end of line}$
     
    40974100\begin{enumerate}[parsep=0pt]
    40984101\item
     4102\Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} reads the newline character, when reading single characters.
     4103\item
     4104\Indexc{nlOff}\index{manipulator!nlOff@©nlOff©} does \emph{not} read the newline character, when reading single characters.
     4105\item
    40994106\Indexc{nl}\index{manipulator!nl@©nl©} scans characters until the next newline character, \ie ignore the remaining characters in the line.
    4100 \item
    4101 \Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} reads the newline character, when reading single characters.
    4102 \item
    4103 \Indexc{nlOff}\index{manipulator!nlOff@©nlOff©} does \emph{not} read the newline character, when reading single characters.
     4107If ©nlOn© is enabled, the ©nl© is also consumed.
    41044108\end{enumerate}
    41054109For example, in:
     
    41134117For output:
    41144118\begin{enumerate}[parsep=0pt]
     4119\item
     4120\Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} implicitly prints a newline at the end of each output expression.
     4121\item
     4122\Indexc{nlOff}\index{manipulator!nlOff@©nlOff©} does \emph{not} implicitly print a newline at the end of each output expression.
    41154123\item
    41164124\Indexc{nl}\index{manipulator!nl@©nl©} inserts a newline.
     
    412941376
    41304138\end{cfa}
    4131 Note, a terminating ©nl© is merged (overrides) with the implicit newline at the end of the ©sout© expression, otherwise it is impossible to print a single newline
    4132 \item
    4133 \Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} implicitly prints a newline at the end of each output expression.
    4134 \item
    4135 \Indexc{nlOff}\index{manipulator!nlOff@©nlOff©} does \emph{not} implicitly print a newline at the end of each output expression.
     4139Note, a terminating ©nl© is merged with (overrides) the implicit newline at the end of the ©sout© expression, otherwise it is impossible to print a single newline
    41364140\end{enumerate}
    41374141
     
    41394143\subsection{Output Value Manipulators}
    41404144
    4141 The following \Index{manipulator}s control formatting of output values (printing), and only affect the format of the argument.
     4145The following \Index{manipulator}s control formatting (printing) of the argument output values.
    41424146\begin{enumerate}
    41434147\item
     
    41544158\begin{cfa}[belowskip=0pt]
    41554159sout | oct( 0 ) | oct( 27HH ) | oct( 27H ) | oct( 27 ) | oct( 27L );
    4156 0 033 033 033 033
     4160®0® 033 033 033 033
    41574161sout | oct( -27HH ) | oct( -27H ) | oct( -27 ) | oct( -27L );
    415841620345 0177745 037777777745 01777777777777777777745
     
    41644168\begin{cfa}[belowskip=0pt]
    41654169sout | hex( 0 ) | hex( 27HH ) | hex( 27H ) | hex( 27 ) | hex( 27L );
    4166 0 0x1b 0x1b 0x1b 0x1b
     41700x0 0x1b 0x1b 0x1b 0x1b
    41674171sout | hex( -27HH ) | hex( -27H ) | hex( -27 ) | hex( -27L );
    416841720xe5 0xffe5 0xffffffe5 0xffffffffffffffe5
    41694173
    41704174sout | hex( 0.0 ) | hex( 27.5F ) | hex( 27.5 ) | hex( 27.5L );
    4171 0x0.p+0 0x1.b8p+4 0x1.b8p+4 0xd.cp+1
     41750x0p+0 0x1.b8p+4 0x1.b8p+4 0xd.cp+1
    41724176sout | hex( -27.5F ) | hex( -27.5 ) | hex( -27.5L );
    41734177-0x1.b8p+4 -0x1.b8p+4 -0xd.cp+1
     
    44134417Instead, the next sequence of non-whitespace characters are read, and the input sequence is terminated with delimiter ©'\0'©.
    44144418The string variable \emph{must} be large enough to contain the input sequence.
    4415 
    4416 The following \Index{manipulator}s control formatting of input values (reading), and only affect the format of the argument.
    4417 
     4419To force programmers to consider buffer overruns for C-string input, C-strings can only be read with a width field, which should specify a size less than or equal to the C-string size, \eg:
     4420\begin{cfa}
     4421char line[64];
     4422sin | wdi( ®sizeof(line)®, line ); // must specify size
     4423\end{cfa}
     4424
     4425A scanset is a simple regular expression, where the matching set contains any Latin-1 character (8-bits) or character ranges using minus.
     4426For example, the scanset \lstinline{"a-zA-Z -/?§"} matches characters between ©'a'© and ©'z'©, between ©'A'© and ©'Z'©, between space and ©'/'©, and characters ©'?'© and (Latin-1) ©'§'©.
     4427The following string is matched by this scanset:
     4428\begin{cfa}
     4429!&%$  abAA () ZZZ  ??  xx§§§§
     4430\end{cfa}
     4431To match a minus, put it as the first character, ©"-0-9"©.
     4432Note, other complex forms of regular-expression matching is not supported.
     4433
     4434The following \Index{manipulator}s control scanning of input values (reading), and only affect the format of the argument.
    44184435\begin{enumerate}
    44194436\item
     
    44584475
    44594476\item
    4460 \Indexc{incl}( scanset, input-string )\index{manipulator!incl@©incl©}
    4461 For C-string types, the scanset matches any number of characters \emph{in} the set.
     4477\Indexc{incl}( scanset, wdi-input-string )\index{manipulator!incl@©incl©}
     4478For C-string types only, the scanset matches any number of characters \emph{in} the set.
    44624479Matching characters are read into the C input-string and null terminated.
    44634480\begin{cfa}[belowskip=0pt]
     
    44704487
    44714488\item
    4472 \Indexc{excl}( scanset, input-string )\index{manipulator!excl@©excl©}
     4489\Indexc{excl}( scanset, wdi-input-string )\index{manipulator!excl@©excl©}
    44734490For C-string types, the scanset matches any number of characters \emph{not in} the set.
    44744491Non-matching characters are read into the C input-string and null terminated.
     
    44804497®xyz®bca
    44814498\end{cfa}
     4499
     4500\item
     4501\Indexc{getline}( char delimit, wdi-input-string )\index{manipulator!getline@©getline©}
     4502Is an @excl@ with scanset ©"delimit"©, which consumes all characters up to the delimit character.
     4503If the delimit character is omitted, it defaults to ©'\n'© (newline).
    44824504\end{enumerate}
    44834505
Note: See TracChangeset for help on using the changeset viewer.