C++ file input and output are typically achieved by using an object of one of the following classes:
- ifstream for reading input only.
- ofstream for writing output only.
- fstream for reading and writing from/to one file.
All three classes are defined in <fstream.h>. Throughout this page, the term "file stream" will be used when referring to features that apply equally to all three classes. Normally, for binary file i/o you do not use the conventional text-oriented << and >> operators! It can be done, but that is an advanced topic.
Basic Model for File I/O
In C++, the file stream classes are designed with the idea that a file should simply be viewed as a stream or array of uninterpreted bytes. For convenience, the "array" of bytes stored in a file is indexed from zero to len-1, where len is the total number of bytes in the entire file. Each open file has two "positions" associated with it:
- The current reading position, which is the index of the next byte that will be read from the file. This is called the "get pointer" since it points to the next character that the basic get method will return.
- The current writing position, which is the index of the byte location where the next output byte will be placed. This is called the "put pointer" since it points to the location where the basic put method will place its parameter.
These two file positions are independent, and either one can point anywhere at all in the file.
Getting The Size of a File
The typical way to get the size of a file is to use the C library function stat: #include <sys/stat.h>
...
struct stat results;
if (stat("input.bin", &results) == 0)
// The size of the file in bytes is in
// results.st_size
else
// An error occurredNote that the second parameter to stat is a pointer. It is your responsibility to create and manage the memory where stat will place its results, and the address of that memory is what you should pass in as this second parameter. The above example shows the use of a local variable to hold the results returned by stat.
Opening a File
A file stream object can be opened in one of two ways. First, you can supply a file name along with an i/o mode parameter to the constructor when declaring an object: ifstream myFile ("data.bin", ios::in | ios::binary);Alternatively, after a file stream object has been declared, you can call its open method: ofstream myFile;
...
myFile.open ("data2.bin", iosut | ios::binary);Either approach will work with an ifstream, an ofstream, or an fstream object. Normally, when manipulating text files, one omits the second parameter (the i/o mode parameter). However, in order to manipulate binary files, you should always specify the i/o mode, including ios::binary as one of the mode flags. For read/write access to a file, use an fstream:
fstream myFile;
myFile.open ("data3.bin", ios::in | iosut | ios::binary);




ut | ios::binary);Either approach will work with an ifstream, an ofstream, or an fstream object. Normally, when manipulating text files, one omits the second parameter (the i/o mode parameter). However, in order to manipulate binary files, you should always specify the i/o mode, including ios::binary as one of the mode flags. For read/write access to a file, use an fstream:
.de
Reply With Quote