Sequential Files : Text File « File « C++






Sequential Files

  
#include <fstream>
#include <iostream>
using namespace std;
int main() 
{
   char buffer[256];

   fstream myfile;
   myfile.open("test2.txt",ios::out | ios::trunc);
   if (myfile.is_open())
   {
        myfile << "This outputting a line.\n";
        myfile.close();
   }
   myfile.open("test.txt",ios::in);
   myfile.getline(buffer,100);
   cout << "The file contains   " << buffer << "\n";
   myfile.close();
   myfile.open("test.txt",ios::app);
   myfile << " Hey this is another line \n";
   myfile.close();
   myfile.open("test.txt",ios::in);
   myfile.getline(buffer,200);
   cout << "The file contains   " << buffer << "\n";
   myfile.close();
   return 0;
}
  
    
  








Related examples in the same category

1.Use the getline function with the C++ string classUse the getline function with the C++ string class
2.Write strings to disk
3.Write string to a file
4.Write to file: ofstream
5.Read from file.Read from file.
6.Reading from a File
7.Writing to a File
8.reading a text file
9.writing on a text file