Next: , Previous: I/O utilities, Up: I/O utilities


4.7.1 Opening and closing binary files

As with standard Unix, a file must be opened before reading or writing. However, we distinguish file handles for both cases.

Open a file for reading

Syntax QDP_Reader *QDP_open_read(QDP_String *md, char *filename);


Meaning Opens a named file for reading and reads the file metadata.


Example QDP_Reader *infile;
QDP_String *file_xml = QDP_string_create();
infile = QDP_open_read(file_xml, filename);

The QDP_Reader return value is the file handle used in subsequent references to the file. A null return value signals an error. The I/O system takes responsibility for allocating and freeing space for the handle. It is assumed the user has preperly created the QDP_String that will hold the file metadata so it can be read from the head of the file and inserted. The volume format (see QDP_open_write below) is autodetected, so is not specified.

Open a file for writing

Syntax QDP_Writer *QDP_open_write(QDP_String *md, char *filename, int volfmt);


Meaning Opens a named file for writing and writes the file metadata.


Example QDP_Writer *outfile;
QDP_String *file_xml = QDP_string_create();
QDP_string_set(file_xml, xml_string);
outfile = QDP_open_write(file_xml, filename, QDP_SINGLEFILE);

The QDP_Writer return value is the file handle used in subsequent references to the file. A null return value signals an error. The I/O system takes responsibility for allocating and freeing space for the handle. It is assumed the user has already created the file metadata in file_xml, so it can be written at the head of the file. The volfmt argument is either QDP_SINGLEFILE or QDP_MULTIFILE. QDP_SINGLEFILE creates a single file for the output from all nodes while QDP_MULTIFILE creates one file per node.

Close an input file

Syntax int QDP_close_read(QDP_Reader *reader);


Meaning Closes an input file.


Example QDP_close_read(reader);

Close an output file

Syntax int QDP_close_write(QDP_Writer *writer);


Meaning Closes an output file.


Example QDP_close_write(writer);

In both cases the integer return value is 0 for success and 1 for failure.