| Start | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Writing to a file There are several ways to write to a file. The easiest way is to use an output file stream ( Instead of After writing to a stream, you should always check if error state flag |
||||||||||||||||||||||||||||
| Opening a file Opening a file is done in the same way for all 3 file streams ( You can open the file directly in the constructor: Alternatively, you can use the file stream's member function You should always check if a file has been opened successfully (even when writing). Failures can include: the file doesn't exist, file hasn't the right access rights, file is already in use, disk errors occurred, drive disconnected ... Checking can be done as follows: When file path contains backslashes (for example, on Windows system) you should properly escape them: or use raw literal: or use forward slashes instead: If you want to open file with non-ASCII characters in path on Windows currently you can use non-standard wide character path argument: |
||||||||||||||||||||||||||||
| Reading from a file 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 ( Then you can use the following code to read that data from the file: The stream extraction operator
This means that the following version of the file foo.txt will also be successfully read by the previous code: The stream extraction operator If you wish to read an entire file as a string, you may use the following code: This code reserves space for the If you want to read a file line by line, you can use the function If you want to read a fixed number of characters, you can use the stream's member function After executing a read command, you should always check if the error state flag |
||||||||||||||||||||||||||||
| Opening modes 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 An opening mode can be provided as second parameter to the constructor of a file stream or to its It is to be noted that you have to set If you don't specify an opening mode, then the following default modes are used:
The file opening modes that you may specify by design are:
Note: Setting the For example on Windows the end of line sequence is CRLF ( |
||||||||||||||||||||||||||||
Reading an ASCII file into a std::string The Another possibility (popularized in Effective STL (opens new window) by Scott Meyers (opens new window)) is: This is nice because requires little code (and allows reading a file directly into any STL container, not only strings) but can be slow for big files. NOTE: the extra parentheses around the first argument to the string constructor are essential to prevent the most vexing parse problem. Last but not least: which is probably the fastest option (among the three proposed). |
||||||||||||||||||||||||||||
| Writing files with non-standard locale settings If you need to write a file using different locale settings to the default, you can use Guidance for use:
Reasons for Restrictions: Imbuing a file stream with a locale has undefined behavior if the current locale is not state independent or not pointing at the beginning of the file. UTF-8 streams (and others) are not state independent. Also a file stream with a UTF-8 locale may try and read the BOM marker from the file when it is opened; so just opening the file may read characters from the file and it will not be at the beginning. Explicitly switching to the classic "C" locale is useful if your program uses a different default locale and you want to ensure a fixed standard for reading and writing files. With a "C" preferred locale, the example writes If, for example, the preferred locale is German and hence uses a different number format, the example writes (note the decimal comma in the first line). |
||||||||||||||||||||||||||||
| Flushing a stream 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 invoking the There is a stream manipulator Buffering can improve the performance of writing to a stream. Therefore, applications that do a lot of writing should avoid flushing unnecessarily. Contrary, if I/O is done infrequently, applications should consider flushing frequently in order to avoid data getting stuck in the stream object. |
||||||||||||||||||||||||||||
| Reading a file into a container In the example below we use In the above example we are simply iterating through the file reading one "item" at a time using We can extend this to read any object types we like by simply specifying the object we want to read as the template parameter to the |
||||||||||||||||||||||||||||
| Checking end of file inside a loop condition, bad practice?
You could correctly write: but is simpler and less error prone. Further references:
|
||||||||||||||||||||||||||||
| Closing a file 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 done by putting all file operations into an own scope ( Calling |
||||||||||||||||||||||||||||
Reading a struct from a formatted text file. file4.txt Output: |
||||||||||||||||||||||||||||
Copying a file With C++17 the standard way to copy a file is including the The filesystem library was originally developed as |
Read Article
https://develop.horje.com/learn/1434/reference