Reading Files in C++ Our first example demonstrates reading files in C++. Reading and writing files are basic tasks needed for many C++ programs. Let’s look at some examples of reading files.
#include <iostream>
#include <fstream>
#include <vector>
#include <stdexcept>
// Helper function to check for errors
void check ( bool condition , const std :: string & message ) {
if ( ! condition ) {
throw std :: runtime_error ( message );
}
}
int main () {
// Perhaps the most basic file reading task is
// slurping a file's entire contents into memory.
std :: ifstream file ( "/tmp/dat" );
check ( file . is_open (), "Failed to open file" );
std :: string content (( std :: istreambuf_iterator < char > ( file )),
std :: istreambuf_iterator < char > ());
std :: cout << content ;
// You'll often want more control over how and what
// parts of a file are read. For these tasks, we can use
// various methods of std::ifstream.
file . clear ();
file . seekg ( 0 , std :: ios :: beg );
// Read some bytes from the beginning of the file.
// Allow up to 5 to be read but also note how many
// actually were read.
std :: vector < char > b1 ( 5 );
file . read ( b1 . data (), 5 );
int n1 = file . gcount ();
std :: cout << n1 << " bytes: " << std :: string ( b1 . begin (), b1 . begin () + n1 ) << " \n " ;
// You can also seek to a known location in the file
// and read from there.
file . seekg ( 6 , std :: ios :: beg );
std :: vector < char > b2 ( 2 );
file . read ( b2 . data (), 2 );
int n2 = file . gcount ();
std :: cout << n2 << " bytes @ 6: " << std :: string ( b2 . begin (), b2 . begin () + n2 ) << " \n " ;
// Other methods of seeking are relative to the
// current position or the end of the file.
file . seekg ( 4 , std :: ios :: cur );
file . seekg ( - 10 , std :: ios :: end );
// The C++ standard library provides some functions that may
// be helpful for file reading. For example, we can use
// std::getline to read a line from the file.
file . seekg ( 6 , std :: ios :: beg );
std :: string line ;
std :: getline ( file , line );
std :: cout << line . length () << " bytes @ 6: " << line << " \n " ;
// There is no built-in rewind, but
// seekg(0, std::ios::beg) accomplishes this.
file . seekg ( 0 , std :: ios :: beg );
// The C++ standard library also provides a buffered reader
// in the form of std::istream. It's already being used by
// std::ifstream, so we don't need to create a separate one.
char b4 [ 5 ];
file . read ( b4 , 5 );
std :: cout << "5 bytes: " << std :: string ( b4 , 5 ) << " \n " ;
// Close the file when you're done (this is done automatically
// when the std::ifstream object goes out of scope).
file . close ();
return 0 ;
}
To run this program, you would first need to create a file at /tmp/dat
with some content. Then compile and run the C++ program:
$ echo "hello" > /tmp/dat
$ echo "cpp" >> /tmp/dat
$ g++ reading-files.cpp -o reading-files
$ ./reading-files
hello
cpp
5 bytes: hello
2 bytes @ 6: cp
2 bytes @ 6: cpp
5 bytes: hello
This example demonstrates various ways to read files in C++, including reading the entire file at once, reading specific portions, seeking to different positions, and using buffered reading. The C++ standard library provides powerful tools for file I/O through the <fstream>
header.
Next, we’ll look at writing files in C++.