Search⌘ K

File Streams

Explore the use of file streams in C++ to manage file input and output efficiently. Understand the different file stream classes, opening modes, and methods to control file position pointers. Learn how to open, read, write, and close files safely while handling both char and wchar_t data types. This lesson provides essential skills for working with files and file buffers in professional C++ programming.

We'll cover the following...

File streams enable us to work with files. They need the header <fstream>. The file streams automatically manage their file for its whole lifetime.

Whether we use a file stream for input, output, or with the character type char or wchar_t there are various file stream classes:

Class Use
std::ifstream and std::wifstream File stream for the input of data of type char and wchar_t.
std::ofstream and std::wofstream File stream for the output of data of type char and wchar_t.
std::fstream and std::wfstream File stream for the input and output of data of type char and wchar_t.
std::filebuf and std::wfilebuf Data buffer of type char and wchar_t.

⚠️ Set the file position pointer
File streams used for reading and writing have to set the file position pointer after the contents change. ...