Pages

Sunday, June 22, 2014

File Operations in C++

The header file which contains the library for file operations is fstream.
 Be sure to include this line at the top.

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.


#include <iostream>
#include <string>
#include <fstream>
using namespace std; int main() {

  fstream myfile;                                                 
  string data;
  
  myfile.open ("B-large-practice.in");                             
  getline(myfile,data);                                                          
  cout << data<< endl;
  while(!getline(myfile,data).eof()){
 

  int no_of_cases=data.length();

  cout << no_of_cases<
  
  }

  myfile.close();                         

getchar();
return 0;
}