Opening Files for Read and Write : File Read Write « File « C++






Opening Files for Read and Write

  
#include <fstream>
#include <iostream>


using namespace std;
int main()
{
   char buffer[255];    

   ofstream fout("text.txt");  
   fout << "This line written directly to the file...\n";
   cout << "Enter text for the file: ";
   cin.ignore(1,'\n');
   cin.getline(buffer,255);

   fout << buffer << "\n";
   fout.close();         

   ifstream fin("text.txt");    
   char ch;
   while (fin.get(ch))
      cout << ch;

   fin.close();
   return 0;
}
  
    
  








Related examples in the same category

1.Reads information from the file and outputs it onto the screenReads information from the file and outputs it onto the screen
2.Another example of read() and write() and illustrates the use of gcount( )Another example of read() and write() and illustrates the use of gcount( )
3.read( ), write( ) and gcount( ):
4.Use get() and getline() to read characters.