Read / Write Commands

Read / Write Commands

Methods of reading and writing data from / to files in import and export filters. Many commands here use formatting strings to provide formatted input and output. All reading and writing commands here work on input or output files as defined internally by the program.

[Note]See also:

addreadoption

Syntax:

void addreadoption(option); 
string  option;

Controls aspects of file reading. See Parse Options for a list of possible options.

For example:

addreadoption("stripbrackets");

eof

Syntax:

int eof(); 

Returns 1 if at the end of the current input file, 0 otherwise.

For example:

string s;
while (!eof())
{
	getline(s):
	printf("%s\n", s);
} 

Reads in and prints out all lines in the current source file.

filterfilename

Syntax:

string filterfilename(); 

Returns the name of the current input or output file (typically useful from within an import or export filter).

For example:

string filename = filterfilename();

Puts the current source/destination filename in the variable filename.

find

Syntax:

int find(searchstring); 
string  searchstring;
int find(searchstring,  
 linevar); 
string  searchstring;
string  linevar;

Searches for the specified searchstring in the input file, returning 0 if searchstring is not found before the end of the file, and 1 if it is. The optional argument linevar is a character variable in which the matching line (if any) is put.

For example:

int iresult = find("Final Energy:");

searches for the string 'Final Energy' in the input file, placing the result of the search in the variable iresult.

string line;
int n = find("Optimised Geometry:", line);

searches for the string 'Optimised Geometry:' in the input file, placing the whole of the matching line from the input file in the variable line.

getline

Syntax:

int getline(destvar); 
string  destvar;

Read an entire line from the input file, and put it in the character variable provided. The line also becomes the current target for readnext. The command returns a Read Successinteger.

For example:

string nextline;
int n = getline(nextline);

gets the next line from the file and places it in the variable nextline.

nextarg

Syntax:

int nextarg(i); 
int  i;
int nextarg(d); 
double  d;
int nextarg(s); 
string  s;

Read the next whitespace-delimited chunk of text from the current file and place it in the variable supplied. Note that this command reads directly from the file and not the last line read with getline or readline (see the readnext command to read the next delimited argument from the last read line). The command returns TRUE (1) if an argument was successfully read, or FALSE (0) otherwise (e.g. if the end of the file was found).

The command will accept a variable of any ordinary type - int, double, or string - as its argument. Conversion between the text in the file and the target variable type is performed automatically.

For example:

int i;
int success = nextarg(i);

peekchar

Syntax:

string peekchar(); 

Peeks the next character that will be read from the source file, and returns it as a string. The actual file position for reading is unaffected.

For example:

string char = peekchar();

peekchari

Syntax:

int peekchari(); 

Peeks the next character that will be read from the source file, and returns it as an integer value representing the ASCII character code (see http://www.asciitable.com, for example). The actual file position for reading is unaffected.

For example:

int char = peekchari();

readchars

Syntax:

string readchars(nchars,  
 skipeol = TRUE); 
int  nchars;
bool  skipeol = TRUE;

Reads and returns (as a string) a number of characters from the input file. If skipeol is true (the default) then the end-of-line markers '\n' and '\r' will be ignored and will not count towards nchars - this is of most use when reading formatted text files and you want to 'ignore' the fact that data is presented on many lines rather than one. If skipeol is false then '\n' and '\r' will count towards the total number of characters. Used on formatted text files, this might give you unexpected results.

For example:

string text = readchars(80);

reads the next 80 characters from the input file and puts it into the variable text.

readdouble

Syntax:

double readdouble(); 
double readdouble(nbytes); 
int  nbytes;

Read a floating point value (the size determined from the machines 'double' size) from an unformatted (binary) input file. Alternatively, if a valid number of bytes is specified and corresponds to the size of another 'class' of double (e.g. long double) on the machine this size is used instead.

For example:

double x = readdouble();

reads a floating point value into the variable x.

readdoublearray

Syntax:

int readdoublearray(d[],  
 n); 
double  d[];
int  n;

Read n consecutive integer values (whose individual size is determined from the result of calling 'sizeof(double)') from an unformatted (binary) input file, placing in the array d provided. The size of the array provided must be at least n. The command returns a Read Success integer.

For example:

double data[45];
int success = readdoublearray(data, 45);

reads 45 double numbers into the array data.

readint

Syntax:

int readint(); 
int readint(nbytes); 
int  nbytes;

Read an integer value (the size determined from the result of calling 'sizeof(int)') from an unformatted (binary) input file. Alternatively, if a valid number of bytes is specified and corresponds to the size of another 'class' of int (e.g. long int) on the machine this size is used instead.

For example:

int i = readint();

reads an integer number into the variable i.

readintarray

Syntax:

int readintarray(i[],  
 n); 
int  i[];
int  n;

Read n consecutive integer values (whose individual size is determined from the result of calling 'sizeof(int)') from an unformatted (binary) input file, placing in the array i provided. The size of the array provided must be at least n. The command returns a Read Success integer.

For example:

int data[100];
int success = readintarray(data, 100);

reads 100 integer numbers into the array data.

readline

Syntax:

int readline(var,  
 ...); 
int|double|string  var;
 ...;

Read a line of delimited items from the input file, placing them into the list of variable(s) provided. Conversion of data from the file into the types of the destination variables is performed automatically. The number of items parsed successfully is returned.

For example:

double x,y,z;
int n = readline(x,y,z);

reads a line from the file and places the first three delimited items on the line into the variables x, y, and z.

readlinef

Syntax:

int readlinef(format,  
 ...); 
string  format;
 ...;

Read a line of data from the input file and separate them into the list of variable(s) provided, and according to the format provided. The number of items parsed successfully is returned.

For example:

double x,y,z;
int n = readlinef("%8.6f %8.6f %8.6f",x,y,z);

reads a line from the file, assuming that the line contains three floating point values of 8 characters length, and separated by a space, into the three variables x, y, and z.

readnext

Syntax:

int readnext(i); 
int  i;
int readnext(d); 
double  d;
int readnext(s); 
string  s;

Read the next delimited argument from the last line read with either getline or readline into the variable supplied. The command returns either TRUE for success or FALSE (e.g. if the end of file was reached without reading any non-whitespace characters, or an error was encountered).

For example:

double d;
int n = readnext(d);

read the next delimited argument into the double variable d.

readvar

Syntax:

int readvar(source,  
 var,  
 ...); 
string  source;
int|double|string  var;
 ...;

Parse the contents of the supplied string source into the supplied variables, assuming delimited data items. Delimited items read from source are converted automatically to the type inferred by the target variable. The number of data items parsed successfully is returned.

For example:

string data = "rubbish ignore  Carbon  green  1.0 2.5 5.3";
string element, discard;
vector v;
int n = readvar(data,discard,discard,element,discard,v.x,v.y,v.z,discard);
printf("Element = %s, n = %i\n", element, n);

outputs

Element = Carbon, n = 7

The character string in the variable data is parsed, with delimited chunks placed into the supplied variables. Note the repeated use of the variable discard, used to get rid of unwanted data. Also, note that there are not enough items in data to satisfy the final occurrence of discard, and so the function returns a value of 7 (as opposed to the actual number of target variables supplied, 8).

readvarf

Syntax:

int readvarf(source,  
 format,  
 var,  
 ...); 
string  source;
string  format;
int|double|string  var;
 ...;

Parse the contents of the supplied string source according to the supplied format string, placing in the supplied variables. The number of format specifiers successfully parsed (or, to look at it another way, the number of the supplied variables that were assigned values) is returned.

For example:

string a, b, data = "abc def123456.0";
double d; int i, n;
n = readvarf(data,"%3s %3s%4i%4f%8*",a,b,i,d);
printf("a = %s, b = %s, d = %f, i = %i, n = %i\n", a, b, d, i, n);

outputs

a = abc, b = def, d = 56.000000, i = 1234, n = 4

The supplied format string contains a single space in between the two '%3s' specifiers, and is significant since it corresponds to actual (discarded) space when processing the format. Furthermore, the last specifier '%8*' (discard 8 characters) is not fulfilled by the data string, and so the number of arguments successfully parsed is 4, not 5.

removereadoption

Syntax:

void removereadoption(option); 
string  option;

Removes a previously-set read option. See Parse Options for a list of possible options.

For example:

removereadoption("skipblanks");

rewind

Syntax:

void rewind(); 

Rewind the input file to the beginning of the file.

For example:

rewind();

skipchars

Syntax:

void skipchars(n); 
int  n;

Skips the next n characters in the input file.

For example:

skipchars(15);

discards the next 15 characters from the input file.

skipline

Syntax:

void skiplin(n = 1); 
int  n = 1;

Skips the next line in the file, or the next n lines if a number supplied.

For example:

skipline();

skips the next line in the file.

skipline(5);

discards 5 lines from the file.

writeline

Syntax:

void writeline(var,  
 ...); 
int|double|string  var;
 ...;

Write a line to the current output file that consists of the whitespace delimited contents of the supplied arguments. The contents of the arguments are formatted according to their type and suitable internal defaults. A newline character is appended automatically to the end of the written line.

For example:

writeline("Number of atoms =", aten.model.natoms);

writes a line indicating the number of atoms in the model to the current output file.

writelinef

Syntax:

void writelinef(format,  
 ...); 
string  format;
 ...;

Write a formatted line to the current output file, according to the supplied format and any supplied arguments. Usage is the same as for the printf command. Note that a newline character is not automatically appended to the end of the written line, and one should be written explicitly using the escape sequence '\n'.

For example:

writelinef("%s = %8i\n", "Number of atoms", aten.model.natoms);

writes a line indicating the number of atoms in the model to the current output file, e.g.:

Number of atoms =        3

writevar

Syntax:

void writevar(dest,  
 ...); 
string  dest;
 ...;

Write to the supplied string variable dest the whitespace delimited contents of the supplied arguments. The contents of the arguments are formatted according to their type and suitable internal defaults. A newline character is appended automatically to the end of the written line.

For example:

string s;
writevar(s,"Number of atoms =", aten.model.natoms);
writeline(s);

same result as the example for the 'writeline' command, except that the final string is written to a variable first, and then the file.

writevarf

Syntax:

void writevarf(dest,  
 format,  
 ...); 
string  dest;
string  format;
 ...;

Write to the supplied string variable dest the string resulting from the supplied format and any other supplied arguments. Apart from the mandatory first argument being the destination string variable, usage is otherwise the same as the printf command. Note that a newline character is not automatically appended to the end of the written line, and one should be written explicitly using the escape sequence '\n'.

For example:

string s;
writevarf(s,"%s = %8i\n", "Number of atoms", aten.model.natoms);
writeline(s);

same result as the example for the 'writelinef' command, except that the final string is written to a variable first, and then the file.