File IO

From K-3D

Jump to: navigation, search


Excepting code that has to integrate with third-party libraries (such as libtiff), all file I/O should be done using the K-3D wrappers for std::fstream:

#include <k3dsdk/fstream.h>

k3d::filesystem::ifstream input_stream("/path/to/foo");
k3d::filesystem::ofstream output_stream("/path/to/bar");

These streams differ from the standard fstreams in in that they always open in binary mode. This has no effect on input or output on Posix systems, but it ensures that data is written and read without modification on Win32.

The K-3D streams also use k3d::filesystem::path for their input arguments, which is consistent with the requirement that all path manipulation and storage in K-3D is done with k3d::filesystem::path (for portability and support of i18n).

For file formats that accept either Unix or DOS line endings, prefer to write Unix endings. Write code to handle either Unix or DOS line endings when reading.


For compressed file I/O, use the K-3D wrappers for gzip:

#include <k3dsdk/gzstream.h>

k3d::filesystem::igzstream input_stream("/path/to/foo.gz");
k3d::filesystem::ogzstream output_stream("/path/to/bar.gz");

Note that k3d::filesystem::igzstream will transparently handle uncompressed files, so no logic is required to identify compressed files before reading.

Personal tools