Sunday, January 10, 2021

C++ program to write and read text in/from file.


 //C++ program to write and read text in/from file.


#include <iostream>
#include <fstream>
 
using namespace std;
 
int main()
{
   fstream file; //object of fstream class
    
   //opening file "sample.txt" in out(write) mode
   file.open("shri.txt",ios::out);
    
   if(!file)
   {
       cout<<"Error in creating file!!!"<<endl;
       return 0;
   }
    
   cout<<"File created successfully."<<endl;
                    //write text into file
   file<<"Welcome to Matoshri Engineering college, Nashik";
   //closing the file
   file.close();
    
   //again open file in read mode
   file.open("shri.txt",ios::in);
    
   if(!file)
   {
       cout<<"Error in opening file!!!"<<endl;
       return 0;
   }   
    
   //read untill end of file is not found.
   char ch; //to read single character
   cout<<"File content: ";
    
   while(!file.eof())
   {
       file>>ch; //read single character from file
       cout<<ch;
   }
    
   file.close(); //close file
    
   return 0;
}

1 comment:

  1. Casino Player Profile: Casinos, Gaming, Slots - DrmCAD
    Learn about Casinos, Gaming, Slots, and bonuses offered by casinos in 당진 출장안마 the Netherlands. Learn more 제천 출장샵 about them 경주 출장안마 in the official 진주 출장마사지 blog. 대전광역 출장샵

    ReplyDelete