C++C++17Introduction to C++Learn C++

How To Read And Write Text Files In A Modern C++ App

How To Read And Write Text Files In A Modern C++ App

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.

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,

What is the correct syntax of ofstream open()?

Since C++17, open() method of ofstream can be written as below,

What is the fstream open() syntax?

Since C++17, open() method of fstream can be written as below,

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,

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,

What about a C++ Input File Stream example?

This block example shows how to read lines from a text file in string form,

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++,

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.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, and Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Generic ProgrammingLearn C++Templates

What Is A Template In C++?

C++C++17Code SnippetComponentsIteratorsLearn C++

What Are The Boost C++ Libraries?

C++C++17Introduction to C++Learn C++Syntax

Learn Bits and How To Use std::bitset In C++

C++C++17IteratorsLearn C++

How To Use Insert In std::map In Modern C++