Reads information from the file and outputs it onto the screen : File Read Write « File « C++






Reads information from the file and outputs it onto the screen

Reads information from the file and outputs it onto the screen
 

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

int main ()
{
   char data[80];
   ofstream outfile;
   outfile.open("file.dat");
   cout << "Writing to the file" << endl;
   cout << "Enter your name: "; 
   cin.getline(data, 80);
   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"); 
   infile >> data; 
   cout << data << endl; 
   infile >> data; 
   cout << data << endl; 
   infile.close();
   return 0;
}

           
         
  








Related examples in the same category

1.Another example of read() and write() and illustrates the use of gcount( )Another example of read() and write() and illustrates the use of gcount( )
2.Opening Files for Read and Write
3.read( ), write( ) and gcount( ):
4.Use get() and getline() to read characters.