File-Handling-in-C++ .

In this session i have discuss file handling in C++ .


A file is a storage medium where we can store the data or information on a disc . It store the data permanently . There are two types of file binary file and text file . We can read the data from file and write the data in file using stream . The stream is sequence of bytes . We perform the operation on file so it is called the file handling .


Types of Stream

There are three types of stream . We use for reading and writing the data . They are as follows .

1-ofstream-It is called the output stream . It is used to write the data in file .

2-ifstream-It is called the input stream . It is used to read the data from file .

3-fstream-It is called the input/output stream both . It is used for read the data from file and write the data in file .


Operation on file

You can perform the following operation on file .

1-Create/open a file .

2-Read data from a file .

3-Write new data in file .

4-close the file .


1-Create/open a file

There are two ways by which you can create/open a file .

1-By constructor .

2-By member function .

1-By constructor

We known the constructor is used to initialize the object . Here a file name is used to initialized the file stream .

a) ofstream outfile("filename")

b) ifstream infile("filename")

a) ofstream demo

Example : Write a program that demonstrate how to create/open file using constructor .

    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
        char name[30],city[30];
        ofstream outfile("e:\\tt.txt");
        cout<<"\n Enter the Name : ";
        
        cin>>name;
        outfile<<name<<"\n"; //Write the name in file
        cout<<"\n Enter the City : ";
        cin>>city;
        outfile<<city<<"\n";//Write the name in file
        cout<<"File successfully created ";
        return 0;
    
    }
    
    OUTPUT : Enter the Name : Shashank
             Enter the City : Noida
             File successfully created
    

In the above program the file is create in e drive of the computer .

b) ifstream demo

Example : Write a program that demonstrate how to read data from file using constructor .

    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
        char name[30],city[30];
        ifstream infile("e:\\tt.txt");
        cout<<"\n Enter the Name : ";
        infile>>name;
        infile>>city;
        cout<<"\n Name : "<<name;
        cout<<"\n City : "<<city;
        return 0;
    }
    
    OUTPUT : Name : Shashank
             City : Noida
    

In the above program data read from e drive file of the computer .

2-By member function

We can also create/open a file using member function .

    open("filename",mode);
    

Where filename is name of file , mode is the optional parameter with a combination of the following flag .

Flag
Description
ios::in Open for input operation
ios::out Open for output operation
ios::binary Open for binary mode .
ios::ate Set the initial position at the end of the file . If flag is not set , the initial position is the beginning of the file .
ios::app Append the content at the end of the file.
ios::trunc Delete the previous data and replace by new one .
.

Example : Write a program that demonstrate how to create/open a file using member function .

    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
        ofstream outfile;
        outfile.open("e:\\tt.txt",ios::out); //open the file
        if(!outfile)
        {
            cout<<"File already exist";
        }
        else
        {
            cout<<"\nFile is created : ";
            outfile<<"Rakesh"; //Write data in file
            outfile.close(); //close the file
        }
        return 0;
    }
    
    OUTPUT : File is created : 
     

In the above program file created on e drive of the computer .

Example : Write a program that demonstrate read the data from file using member function .

    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
        ifstream infile;
        infile.open("e:\\tt.txt",ios::in);
        if(!infile)
        {
            cout<<"\n No such file ";
        }
        else
        {
            char ch;
            while(!infile.eof())
            {
                    infile>>ch;
                    cout<<ch;
            }
        }
        return 0;
    }
    
    OUTPUT : Rakesh  
     

In the above program file created on e drive of the computer .

C++ put function demo

Example : Write a program that demonstrate the put function of ofstream

    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
           ofstream fout;
           char ch;

           fout.open("e:\\tt.txt");			
           do							
           {
                cin.get(ch);
                fout.put(ch);

           }while(ch!='\n');

           fout.close();
           cout<<"\nData written successfully...";
           return 0;
    } 
   OUTPUT : Hello sir
            Data written successfully...
   

C++ get function demo

Example : Write a program that demonstrate the get function of ifstream

    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
          ifstream fin;
          char ch;
          fin.open("e:\\tt.txt");			
          cout<<"\nData in file...\n";
          while(!fin.eof())		
          {
                fin.get(ch);
                cout<<ch;
          }
          fin.close();
    }
   OUTPUT : Data in file...
            Hello sir
   

Welcome to AndroidATPoint.

back image                     next image