C++ files and streams
In C++, a stream is an object that moves data from one place to another.
You already use streams when printing to the terminal:
std::cout is an output stream.
File streams work in a similar way, but the data goes to or comes from a file.
Basic stream types
Include the file stream library:
Common file stream classes:
| Stream | Meaning | Usage |
|---|---|---|
std::ofstream |
output file stream | write to a file |
std::ifstream |
input file stream | read from a file |
std::fstream |
file stream | read and write |
Write to a file
This creates a file called hello.txt and writes text into it.
After running the program, hello.txt contains:
file << value works like std::cout << value, but the output goes to the
file instead of the terminal.
Read from a file
This reads text from hello.txt line by line.
Output:
std::getline(file, line) reads one full line from the file into the string
line.
Check if the file opened
Always check that the file opened successfully.
This is useful when the file does not exist or the program does not have permission to read it.
Append to a file
By default, std::ofstream replaces the old file content.
Use std::ios::app to append to the end of the file:
Each time the program runs, it adds another line to log.txt.
Read simple values
You can also read values using >>.
Assume numbers.txt contains:
Read the two numbers:
Output:
Use >> for simple whitespace-separated values.
Use std::getline() when you want to read full text lines.
Complete simple usage
This program writes a file, then reads it back.
The file streams close automatically when they go out of scope.
Key points
- A stream moves data.
std::coutwrites to the terminal.std::ofstreamwrites to a file.std::ifstreamreads from a file.- Use
<<to write data. - Use
>>to read simple values. - Use
std::getline()to read full lines. - Check
if (!file)before using a file.
Exceptions and closing files
In C++, file streams close themselves automatically when the stream object is destroyed.
This is called RAII:
- The file opens when the stream object is created.
- The file closes when the stream object goes out of scope.
Because of this, you usually do not need to call file.close() manually.
Example:
Even though an exception happens, file still closes automatically.
Why?
Because file is a local object inside the try block. When the exception is
thrown, C++ leaves the block and destroys local objects. The std::ofstream
destructor closes the file.
This is safer than manually calling close() at the end, because manual code may
be skipped when an exception occurs.
Enable file stream exceptions
By default, file streams usually do not throw exceptions when an operation fails. They set error flags instead.
You can tell the stream to throw exceptions:
If missing_file.txt does not exist, file.open() throws an exception.
The file still closes automatically when the file object goes out of scope.
Simple recommended style
For most beginner code, use this style:
Important points:
- Put the file stream inside a scope.
- Check if the file opened.
- Throw or return when opening fails.
- Let the stream close itself automatically.
- Avoid raw file handles when
std::ifstreamandstd::ofstreamare enough.