C++ Files and Streams

Posted on

In C++ programming we’re utilizing the iostream normal library, it supplies cin and cout strategies for studying from enter and writing to output respectively.

To learn and write from a file we’re utilizing the usual C++ library referred to as fstream. Allow us to see the information sorts outline in fstream library is:

Knowledge Sort Description
fstream It’s used to create recordsdata, write information to recordsdata, and browse information from recordsdata.
ifstream It’s used to learn information from recordsdata.
ofstream It’s used to create recordsdata and write information to the recordsdata.

C++ FileStream instance: writing to a file

Let’s see the easy instance of writing to a textual content file testout.txt utilizing C++ FileStream programming.

Output:


C++ FileStream instance: studying from a file

Let’s see the easy instance of studying from a textual content file testout.txt utilizing C++ FileStream programming.

Be aware: Earlier than operating the code a textual content file named as “testout.txt” is should be created and the content material of a textual content file is given under:

Welcome to javaTpoint.

C++ Tutorial.

Output:


C++ Learn and Write Instance

Let’s see the easy instance of writing the information to a textual content file testout.txt after which studying the information from the file utilizing C++ FileStream programming.

Output: