Use the getline function with the C++ string class : Text File « File « C++






Use the getline function with the C++ string class

Use the getline function with the C++ string class
 

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

int main ()
{
   string data;
   ofstream outfile;
   outfile.open("file.dat");
   cout << "Writing to the file" << endl;
   cout << "Enter class name: "; 
   getline(cin, data); 
   outfile << data<< endl;
   cout << "Enter your id: "; 
   cin >> data;
   cin.ignore();
   outfile << data<< endl;
   outfile.close();
   ifstream infile; 
   cout << "Reading from the file" << endl; 
   infile.open("file.dat"); 
   getline(infile, data);
   cout << data << endl; 
   getline(infile, data);
   cout << data << endl; 
   infile.close();
   return 0;
}

           
         
  








Related examples in the same category

1.Write strings to disk
2.Write string to a file
3.Write to file: ofstream
4.Read from file.Read from file.
5.Reading from a File
6.Writing to a File
7.reading a text file
8.writing on a text file
9.Sequential Files