With the help of the C++ Standard library, it can allow us to input-output with files in c++. In a very simple C++ program, the main input and output keywords used were mainly cin/cout.

The main C++ keywords used to perform input-output with files in c++ are:

  • ofstream: write on files
  • ifstream: read from files
  • fstream: reads and wrties from/to files

Here is an example of the ofstream keyboard being applied in the code. When you open up the project directory of this source code below, you will find the textfile created with a line of text written to the file.


//<!--DVFMTSC--> Writing<!--DVFMTSC--> to<!--DVFMTSC--> a<!--DVFMTSC--> textfile

#include<!--DVFMTSC--> <!--DVFMTSC--><iostream<!--DVFMTSC-->>
#include<!--DVFMTSC--> <!--DVFMTSC--><fstream<!--DVFMTSC-->>

using<!--DVFMTSC--> namespace<!--DVFMTSC--> std;

int<!--DVFMTSC--> main()
{
//<!--DVFMTSC--> Create<!--DVFMTSC--> and<!--DVFMTSC--> open<!--DVFMTSC--> the<!--DVFMTSC--> textfile<!--DVFMTSC--> to<!--DVFMTSC--> write
ofstream<!--DVFMTSC--> textfile("textfile.txt");

if(textfile.is_open())
{
textfile<!--DVFMTSC--> <!--DVFMTSC--><<!--DVFMTSC--><"This<!--DVFMTSC--> line<!--DVFMTSC--> of<!--DVFMTSC--> text<!--DVFMTSC--> will<!--DVFMTSC--> show<!--DVFMTSC--> in<!--DVFMTSC--> the<!--DVFMTSC--> text<!--DVFMTSC--> file\n";

//<!--DVFMTSC--> When<!--DVFMTSC--> finished<!--DVFMTSC--> writing,<!--DVFMTSC--> close<!--DVFMTSC--> the<!--DVFMTSC--> text<!--DVFMTSC--> file
textfile.close();
}

//<!--DVFMTSC--> If<!--DVFMTSC--> there<!--DVFMTSC--> is<!--DVFMTSC--> a<!--DVFMTSC--> problem<!--DVFMTSC--> with<!--DVFMTSC--> the<!--DVFMTSC--> text<!--DVFMTSC--> file
else
cout<!--DVFMTSC--> <!--DVFMTSC--><<!--DVFMTSC--><<!--DVFMTSC--> "Unable<!--DVFMTSC--> to<!--DVFMTSC--> open<!--DVFMTSC--> the<!--DVFMTSC--> file";
return<!--DVFMTSC--> 0;
}

Applying ifstream by reading a textfile. This will display the text from the textfile and show it on the black command screen when the program is running.


//<!--DVFMTSC--> Reading<!--DVFMTSC--> from<!--DVFMTSC--> a<!--DVFMTSC--> textfile

#include<!--DVFMTSC--> <!--DVFMTSC--><iostream<!--DVFMTSC-->>
#include<!--DVFMTSC--> <!--DVFMTSC--><fstream<!--DVFMTSC-->>
#include<!--DVFMTSC--> <!--DVFMTSC--><string<!--DVFMTSC-->>

using<!--DVFMTSC--> namespace<!--DVFMTSC--> std;

int<!--DVFMTSC--> main()
{
//<!--DVFMTSC--> Use<!--DVFMTSC--> a<!--DVFMTSC--> string<!--DVFMTSC--> to<!--DVFMTSC--> read<!--DVFMTSC--> each<!--DVFMTSC--> line<!--DVFMTSC--> in<!--DVFMTSC--> the<!--DVFMTSC--> textfile
string<!--DVFMTSC--> line;

//<!--DVFMTSC--> Open<!--DVFMTSC--> the<!--DVFMTSC--> existing<!--DVFMTSC--> textfile
ifstream<!--DVFMTSC--> textfile("textfile.txt");

if(textfile.is_open())
{
while(getline(textfile,<!--DVFMTSC--> line))
{
cout<!--DVFMTSC--> <!--DVFMTSC--><<!--DVFMTSC--><<!--DVFMTSC--> line<!--DVFMTSC--> <!--DVFMTSC--><<!--DVFMTSC--><<!--DVFMTSC--> "\n";
}
textfile.close();
}

//<!--DVFMTSC--> If<!--DVFMTSC--> the<!--DVFMTSC--> file<!--DVFMTSC--> does<!--DVFMTSC--> not<!--DVFMTSC--> exist,<!--DVFMTSC--> display<!--DVFMTSC--> an<!--DVFMTSC--> error<!--DVFMTSC--> message
else
cout<!--DVFMTSC--> <!--DVFMTSC--><<!--DVFMTSC--><<!--DVFMTSC--> "Unable<!--DVFMTSC--> to<!--DVFMTSC--> read<!--DVFMTSC--> the<!--DVFMTSC--> file";
return<!--DVFMTSC--> 0;
}

Now what if we wanted to read and write blocks, rather than just read a single line or character from a file, C++ allows programmers to read and write large chunks of data. Here is an example of reading a series of numbers from a text file.


//<!--DVFMTSC--> Reading<!--DVFMTSC--> a<!--DVFMTSC--> block<!--DVFMTSC--> of<!--DVFMTSC--> data

#include<!--DVFMTSC--> <!--DVFMTSC--><iostream<!--DVFMTSC-->>
#include<!--DVFMTSC--> <!--DVFMTSC--><fstream<!--DVFMTSC-->>
#include<!--DVFMTSC--> <!--DVFMTSC--><string<!--DVFMTSC-->>

using<!--DVFMTSC--> namespace<!--DVFMTSC--> std;

int<!--DVFMTSC--> main()
{
//<!--DVFMTSC--> Data<!--DVFMTSC--> to<!--DVFMTSC--> read<!--DVFMTSC--> in
int<!--DVFMTSC--> data_IN[5];

//<!--DVFMTSC--> Reading<!--DVFMTSC--> from<!--DVFMTSC--> a<!--DVFMTSC--> textfile
ifstream<!--DVFMTSC--> textfile("textfile.txt",<!--DVFMTSC--> ios::in<!--DVFMTSC--> |<!--DVFMTSC--> ios::binary);

if(!textfile)
{
cout<!--DVFMTSC--> <!--DVFMTSC--><<!--DVFMTSC--><<!--DVFMTSC--> "Cannot<!--DVFMTSC--> open<!--DVFMTSC--> the<!--DVFMTSC--> file"<!--DVFMTSC--> <!--DVFMTSC--><<!--DVFMTSC--><<!--DVFMTSC--> endl;
}

for(int<!--DVFMTSC--> i=0;<!--DVFMTSC--> i<!--DVFMTSC--> <!--DVFMTSC--><<!--DVFMTSC--> 5;<!--DVFMTSC--> i++)
{
textfile<!--DVFMTSC--> <!--DVFMTSC-->><!--DVFMTSC-->><!--DVFMTSC--> data_IN[i];
{
//<!--DVFMTSC--> Display<!--DVFMTSC--> values<!--DVFMTSC--> to<!--DVFMTSC--> the<!--DVFMTSC--> screen
cout<!--DVFMTSC--> <!--DVFMTSC--><<!--DVFMTSC--><<!--DVFMTSC--> data_IN[i]<!--DVFMTSC--> <!--DVFMTSC--><<!--DVFMTSC--><<!--DVFMTSC--> "\t";
}
}
}

Share this post