Tutorial by Examples

Opening a file is done in the same way for all 3 file streams (ifstream, ofstream, and fstream). You can open the file directly in the constructor: std::ifstream ifs("foo.txt"); // ifstream: Opens file "foo.txt" for reading only. std::ofstream ofs("foo.txt"); // ...
There are several ways to read data from a file. If you know how the data is formatted, you can use the stream extraction operator (>>). Let's assume you have a file named foo.txt which contains the following data: John Doe 25 4 6 1987 Jane Doe 15 5 24 1976 Then you can use the following...
There are several ways to write to a file. The easiest way is to use an output file stream (ofstream) together with the stream insertion operator (<<): std::ofstream os("foo.txt"); if(os.is_open()){ os << "Hello World!"; } Instead of <<, you can also ...
When creating a file stream, you can specify an opening mode. An opening mode is basically a setting to control how the stream opens the file. (All modes can be found in the std::ios namespace.) An opening mode can be provided as second parameter to the constructor of a file stream or to its open(...
Explicitly closing a file is rarely necessary in C++, as a file stream will automatically close its associated file in its destructor. However, you should try to limit the lifetime of a file stream object, so that it does not keep the file handle open longer than necessary. For example, this can be ...
File streams are buffered by default, as are many other types of streams. This means that writes to the stream may not cause the underlying file to change immediately. In oder to force all buffered writes to take place immediately, you can flush the stream. You can do this either directly by invokin...
std::ifstream f("file.txt"); if (f) { std::stringstream buffer; buffer << f.rdbuf(); f.close(); // The content of "file.txt" is available in the string `buffer.str()` } The rdbuf() method returns a pointer to a streambuf that can be pushed into buffer...
In the example below we use std::string and operator>> to read items from the file. std::ifstream file("file3.txt"); std::vector<std::string> v; std::string s; while(file >> s) // keep reading until we run out { v.push_back(s); ...
C++11 struct info_type { std::string name; int age; float height; // we define an overload of operator>> as a friend function which // gives in privileged access to private data members friend std::istream& operator>>(std::istream& is, info_...
std::ifstream src("source_filename", std::ios::binary); std::ofstream dst("dest_filename", std::ios::binary); dst << src.rdbuf(); C++17 With C++17 the standard way to copy a file is including the <filesystem> header and using copy_file: std::fileystem::copy...
eof returns true only after reading the end of file. It does NOT indicate that the next read will be the end of stream. while (!f.eof()) { // Everything is OK f >> buffer; // What if *only* now the eof / fail bit is set? /* Use `buffer` */ } You could correctly write: ...
If you need to write a file using different locale settings to the default, you can use std::locale and std::basic_ios::imbue() to do that for a specific file stream: Guidance for use: You should always apply a local to a stream before opening the file. Once the stream has been imbued you shoul...

Page 1 of 1