Advertisement



< Prev
Next >



C++ Modifying a file on Disk




In our previous articles, we have explained how to perform read, write and append operation on a file on disk. In this tutorial, we are going to explain how to modify the content of a file on disk. But before we modify the content of an existing file present on the disk, we must open it. For this, the C++ language provides us stream classes used to perform file output/write/modify operations.

In order to perform a file output/write/modify operations, C++ provides us a few file stream classes, such as -
These two file stream classes provides us a function named open(), using which we could provide location of the file stored on the disk to depending on the mode in which we open this file.





Let's read the file mode to use to modify the content of a file without deleting its old content.





Modes to modify data of a file


Let's look at the combination of modes required to modify the content of a file.

File Mode Description
ios::in"
Searches for the file and opens it in read mode only(if the file is found).

ios::out
Searches for the file and opens it in write mode. If the file is found, its content is overwritten. If file is not found, a new file is created. Allows you to write to the file.

"ios::binary"
Searches for the file and opens the file(if the file is found) in a binary mode to perform binary input/output file operations. This mode when used with ios::binary, ios::in and ios::out modes, allows you to modify the content of a file




Advertisement




  • Appending data a file by taking input from a char[] array


  • Let's say that we already have an existing file on disk, named File1.txt, with content :

    File1.txt
    Gender : Male
    Age    : 28
    City   : Tokyo
    Weight : 78.6 Kg

    Now let's try to modify the content of this file by replacing the character 'e' to character 'X'. This file will be searched in the current directory(the same directory where we are going to store the upcoming C++ program.).


    //Modifying the data to a file using fstream class and modes ios::
     
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
    
    //Creating an output stream to append new data to a file
    fstream ofstream_ob;
    
    
    //Opening a file named country1.txt to modify the old content
    ofstream_ob.open("File1.txt", ios::in|ios::out|ios::binary);
    
    
    char ch;
    
    while(ofstream_ob)
    {
    ch = ofstream_ob.get(); 
    	 if(ch =='e')
    	{	
    		//Going one byte position back from the current put pointer position
    		ofstream_ob.seekp(-1, ios::cur);
    		ofstream_ob.put('X');
    	}
    	
    }
    
    //Setting the EOF flag off, to allow the access of file again for reading/writing
    ofstream_ob.clear();
    
    cout<<"The modified content of the file : \n";
    
    //Taking the get pointer at the beginning of the file
    ofstream_ob.seekg(0, ios::beg);
    
    //Reading and displaying the modified file
    while(ofstream_ob)
    {
    ch = ofstream_ob.get(); 
    cout<<ch;
    }
    
    
    ofstream_ob.close();
    
    return 0;
    }

    Output


    Executing this program will modify the content of a file named File2.txt in the current directory, by replacing the character 'e' in it with character 'X' and the content of file looks like -

    The modified content of the file :
    GXndXr : MalX
    AgX    : 28
    City   : Tokyo
    WXight : 78.6 Kg 


    Note :


    Let's understand a few important lines of code -

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement