Advertisement



< Prev
Next >



C++ Appending to a file on Disk




In our last article, we have explained how to write to a file on the disk. In this tutorial, we are going to explain how to append data at the end of an existing file on disk. But before we peform the append operation on a file, we must open it. For this, the C++ language provides us stream classes used to perform file output/write/append operations.

In order to perform a file output/write/append 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 append new data at the end of a file without deleting its old content.





Mode to append data to the end of a file


Let's look at the mode required to append new data to the end of a file.

File Mode Description
ios::app
Searches for the file and opens it in append mode i.e. this mode allows you to append new data to the end of a file. If file is not found, a new file is created.




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 :

    Greetings! Hope you are doing well. 

    Now let's try to write and append data to this file by taking input from an already initialized char[] array, This file will be searched in the current directory(the same directory where we are going to store the upcoming C program.).


    //Appending the data to a file using fstream class and modes ios::app 
     
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
    int size =50;
    
    char str[size] = "Best wishes for all your endeavours!";
    char ch;
    
    
    //Creating an output stream to append new data to a file
    fstream fstream_ob;
    
    
    //Opening a file named File.txt to append new content at its end, using the mode - ios::app
    fstream_ob.open("File.txt", ios::app);
    
    
    //Appending a char array value to the end of the file
    fstream_ob<< str << "\n";
    
    
    //closing the output stream after completing the write operation on the file
    fstream_ob.close();
    
    return 0;
    }

    Output


    Executing this program will append the content of char[] to the end of a file named File2.txt in the current directory, with its content :

    Greetings! Hope you are doing well. Best wishes for all your endeavours!





  • Appending data a file by taking input from user at console


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

    Too much of work and no play made Jack a dull boy. 

    Let us append data to a file on disk by taking inputs from the user at the console, This file will be searched in the current directory(the same directory where we are going to store the upcoming C++ program.).


    //Appending the data to a file using fstream class and modes ios::app 
     
    #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 File.txt to append new content at its end
    ofstream_ob.open("File2.txt", ios::app);
    
    
    int i; 
    cout<<"Enter an int : ";
    cin>>i;
    
    
    //writing an int value to the file
    ofstream_ob<< i << "\n";
    
    
    
    char ch;
    cout<<"Enter a char value : ";
    cin>>ch;
    
    //writing an char value to the file
    ofstream_ob<< ch << "\n";
    
    
    
    float f;
    cout<<"Enter a float value : ";
    cin>>f;
    
    
    //Writing a float value to the file
    ofstream_ob<< f << "\n";
    
    char newline_chr;
    cin.get(newline_chr);
    
    
    char arr[40];
    cout<<"Enter the content of char array : ";
    cin.getline(arr,40);
    
    
    //writing a char array to the file
    ofstream_ob<< arr << "\n";
    
    
    string str;
    cout<<"Enter the content of a string : ";
    getline(cin, str);
    
    //writing a string to the file
    ofstream_ob<< str<< "\n";
    
    
    //closing the output stream after completing the write operation on the file
    ofstream_ob.close();
    
    return 0;
    }

    Output


    Enter an int : 1000
    Enter a char value : B
    Enter a float value : 15.4
    Enter the content of char array : Blue Sky
    Enter the content of a string : Always Shine



    Executing this program will append the content of an int, char, float, char[] and a string to the end of a file named File2.txt in the current directory, with its content :

    Too much of work and no play made Jack a dull boy.1000
    B
    15.4
    Blue Sky
    Always Shine







    Executing this program will append the data entered by the user at the console at the end of a file named File2.txt in the current directory. Let's see the content of this file

    Too much of work and no play made Jack a dull boy. Jack must also enjoy his life.


    In the next tutorial, we are going to explain how to modify the content of an existing file.




    Please share this article -





    < Prev
    Next >
    < C++ Write a File
    File Input Function - get() >



    Advertisement

    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

    PYTHON


    Advertisement