- 1.7
- 1.6
- 1.5
- Beta
- Examples (for 1.6a)
- Misc
Formatting strings can be used to specify the layout of data items on a line when reading or writing data, but if the data are separated by whitespace characters such as spaces or tabs (or, alternatively, commas), such 'delimited' data can be read in more easily. In such cases, it is not necessary to know beforehand the number of characters taken up by each item on the line, since the delimiters separate adjacent data items. A simplified method for reading and writing can be employed in these cases.
Commands providing delimited reading and writing are:
Table 4.11. Delimited read/write commands
| Command | Function |
|---|---|
| readline | Read delimited items from a source file, placing into the variables provided |
| readnext | Read the next delimited item from a source file, placing into the variable provided |
| readvar | Read delimited items from a source variable, placing into the variables provided |
| writeline | Write the supplied items to a single line in the output file, separating them with whitespace |
| writevar | Write the supplied items to a supplied string variable, separating them with whitespace |
Note that all are called the same as their formatted counterparts, but minus the 'f' at the end of the name.
Consider this example datafile:
Na 0.0 1.0 0.0 Cl 1.0 0.0 0.0 Na 0.0 -1.0 0.0
Since the data items (element type and coordinates) are separated by whitespace, we need only provide the target variables to the relevant command - a formatting string, as is demanded by the printf command, is not required. Using the readline command, the following code will parse this data correctly:
double x,y,z;
string el;
while (!eof()) { readline(el,x,y,z); newatom(el,x,y,z); }The variables el, x, y, and z will, at any one time, contain the element type and coordinates from one line of the file. In an analogous manner, the data may be written out again with the corresponding writeline command:
for (atom i = aten.model.atoms; i; ++i) writeline(i.symbol,i,rx,i.ry,i.rz);
Each line will have the individual data items separated by a single space.
The readnext command reads in a single delimited item from a source file, preserving the remainder of the input line for subsequent operations. If there is no data left on the current line, a new line is read and the first delimited item is returned. The example above might be written in a slightly clunkier form as:
double x,y,z;
string el;
while (!eof())
{
readnext(el);
readnext(x);
readnext(y);
readnext(z);
newatom(el,x,y,z);
}For all delimited reading operations, items of data read from the line are converted automatically into the type of the destination variable. So, the atom coordinates read in above, which are put into 'double'-type variables, could equally well be put into string variables. Standard C routines are used to convert data items in this way, and only some conversions make sense. For instance, attempting to read an item which is a proper character string (such as element symbol/name data) into a double or integer variable does not make sense. No error message will be raised, and the variables will likely be set to a value of zero (or whatever passes for 'zero' in the context of the type).
For all delimited writing operations, a suitable standard format specifier is chosen with which to write out the data.

