C++

input and output with files in c++

C++ provides the following classes to perform output and input of characters to/from files:

  • ofstream: Stream class to write on files
  • ifstream: Stream class to read from files
  • fstream: Stream class to both read and write from/to files.

These classes are derived directly or indirectly from the classes istream and ostream. We have already used objects whose types were these classes: cin is an object of class istream and cout is an object of class ostream. Therefore, we have already been using classes that are related to our file streams. And in fact, we can use our file streams the same way we are already used to use cin and cout, with the only difference that we have to associate these streams with physical files. Let’s see an example:

// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}

Writing operations on text files are performed in the same way we operated with cout:

// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}

Reading from a file can also be performed in the same way that we did with cin:

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }
  else 
    cout << "Unable to open file"; 
  return 0;
}

take two numbers from file and print the summation in another file

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
     ifstream ifile ("test.txt");
     ofstream ofile ("testOut.txt");
     int x,y, sum;
    if (ifile.is_open())
   {
      ifile >> x >> y;
      sum = x + y;
      ofile << sum << endl;
      ifile.close();
      ofile.close();
   }
 else
    cout << "can't open this file" << endl;
   return 0;
}

to read string from file: 
std::string item_name;
std::ifstream nameFileout;
nameFileout.open("name2.txt");
while (nameFileout >> item_name)
{
  std::getline(nameFileout, item_name)
}
 nameFileout.close();


to know more about files in c++ visit  
Cplusplus

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s