Fixed size vector file I/O (original) (raw)

next up previous contents
Next: Conversion from general to Up: Fixed size vectors Previous: Fixed size vector products Contents

Fixed size vector file I/O

Gandalf supports both ASCII and binary format file I/O of vectors and matrices. Both formats use standard FILE * file streams. ASCII format is obviously more convenient to use, while binary format is more compact and guarantees no loss of precision when the data is read. To print a 3-vector in ASCII format, use

  Gan_Bool gan_vec3_fprint ( FILE *fp, Gan_Vector3 *p,
                             const char *prefix, int indent, const char *fmt );

prefix is a prefix string to print before the vector itself, indentis the number of spaces to indent the vector by, and fmt is a format string to use when printing the vector, e.g. "%f". So for example

  FILE *pfFile;

  pfFile = fopen ( "/tmp/vectors", "w" );
  gan_vec3_fill_q ( &v3x, 1.0, 2.0, 3.0 );
  gan_vec3_fprint ( pfFile, &v3x, "Example vector", 3, "%f" );

will print the output

     Example vector: 1.000000 2.000000 3.000000

to the file "/tmp/vectors". There is also a version gan_vec3_print() for printing to standard output:

  Gan_Bool gan_vec3_print ( Gan_Vector3 *p,
                            const char *prefix, int indent, const char *fmt );

The corresponding input function is

  Gan_Bool gan_vec3_fscanf ( FILE *fp, Gan_Vector3 *p,
                             char *prefix, int prefix_len )

which reads the vector from the file stream fp into the 3-vector pointer p. It also reads the prefix string (up to the specified maximum length prefix_len), which can be compared with the expected prefix string to check for consistency.

Binary file I/O is handled by the functions gan_vec3_fwrite()and gan_vec3_fread(). To write a 3-vector in binary format use

  Gan_Bool gan_vec3_fwrite ( FILE *fp, Gan_Vector3 *p, gan_ui32 magic_number );

The magic_number takes the same role as the prefix string in gan_vec3_fprint(), and is written into the file so that it can be used later to identify the vector when it is read back using

  Gan_Bool gan_vec3_fread ( FILE *fp, Gan_Vector3 *p, gan_ui32 *magic_number );

Error detection: The I/O routines return a boolean value, returningGAN_TRUE on success, GAN_FALSE on failure, invoking the Gandalf error handle in the latter case.


next up previous contents
Next: Conversion from general to Up: Fixed size vectors Previous: Fixed size vector products Contents

2006-03-17