C++ Files and Streams

Posted on

In C++ programming we’re utilizing the iostream commonplace library, it gives 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 known as fstream. Allow us to see the information varieties outline in fstream library is:

Knowledge Kind Description
fstream It’s used to create information, write information to information, and skim information from information.
ifstream It’s used to learn information from information.
ofstream It’s used to create information and write information to the information.

C++ FileStream instance: writing to a file

Let’s see the straightforward 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 straightforward 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 have to 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 straightforward 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: