How can I read a text file in a C++ app? How can I write to a text file in C++? How can I append to a text file in C++? How can I use open() method for the ifstream? How can I use open() method for the ofstream? How can I use open() method for the fstream? What is the syntax of ofstream open()? What is the syntax of ifstream open()? What is the syntax of fstream open()? How can I write and read to a text file using the latest C++ syntax?
Text files manipulation routines are important for everyday computer usage. We use them to read data, to search a part of data in its data, to obtain values from data, or to record our values as new data. Moreover, many common file types are actually in text form even though it might not be immediately obvious. For example, JSON files, XML files, C source code, C++ source, .H header files, .PAS Pascal program files, and SQL Scripts are all types of ‘plain’ text file. Since the first version of C was released, C and C++ have had a lot of functions to read and write on these text files but you had to be quite careful because it wasn’t difficult to make mistakes which could have some unintended side effects or errors. Today, we have more safer and modern methods to read and write these text files. Let’s see how we can do this.
Table of Contents
What does the fstream library do?
In Modern C++, fstream library is used to read and write files. File Stream classes are used to perform output to a file or to perform input to a file or you can perform both on the same file. Generally, a file can be defined as in one of these kinds below
- ofstream: Output File Stream class to write data to a file
- ifstream: Input Stream class to read data to a file
- fstream: I/O File Stream class to both read and write from/to files.
What is the syntax of ifstream open()?
Since C++17, open() method of ifstream can be written as below,
1 2 3 4 |
void open( const std::filesystem::path::value_type *input_filename, ios_base::openmode mode = ios_base::in ); |
What is the correct syntax of ofstream open()?
Since C++17, open() method of ofstream can be written as below,
1 2 3 4 |
void open( const std::filesystem::path::value_type *output_filename, ios_base::openmode mode = ios_base::out ); |
What is the fstream open() syntax?
Since C++17, open() method of fstream can be written as below,
1 2 3 4 |
void open( const std::filesystem::path::value_type *io_filename, ios_base::openmode mode = ios_base::in|ios_base::out ); |
Whar are the modern C++ headers required to read and write text files?
Generally these methods requires 3 headers for the file stream operations on text files. These are,
1 2 3 4 5 |
#include <iostream> #include <fstream> #include <string> |
Here is a C++ example of using the Output File Stream function
This block example shows how to write lines to a text file in string form,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
std::ofstream ofs; // output file stream ofs.open( "D:\\my_stream_test.txt", std::ios::out | // output file stream std::ios::app | // can append to a existing file std::ios::ate ); // set file cursor at the end if(ofs) { ofs << "This is my first record" << std::endl; ofs.close(); } |
What about a C++ Input File Stream example?
This block example shows how to read lines from a text file in string form,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
std::ifstream ifs; // input file stream std::string str; ifs.open( "D:\\my_stream_test.txt", std::ios::in ); // input file stream if(ifs) { while ( !ifs.eof() ) { std::getline ( ifs, str); std::cout << str << std::endl; } ifs.close(); } |
Here is a full example of reading and writing a text File in Modern C++
Here is a simple example to use ifstream and ofstream in Modern C++,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include <iostream> #include <fstream> #include <string> int main() { //------------- WRTING TO AN OUTPUT FILE STREAM ----------------------------------- std::ofstream ofs; ofs.open( "D:\\my_stream_test.txt", std::ios::out | // output file stream std::ios::app | // can append to a existing file std::ios::ate ); // set file cursor at the end if(ofs) { ofs << "This is my first record" << std::endl; ofs << "This is my second record" << std::endl; ofs << "This is my third record" << std::endl; ofs.close(); } else std:: cout << "Unable to open file to write" << std::endl; //------------- READING FROM AN INPUT FILE STREAM ----------------------------------- std::ifstream ifs; std::string str; ifs.open( "D:\\my_stream_test.txt", std::ios::in ); // input file stream if(ifs) { while ( !ifs.eof() ) { std::getline ( ifs, str); std::cout << str << std::endl; } ifs.close(); } else std:: cout << "Unable to open file to read" << std::endl; getchar(); return 0; } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition