Advertisement



< Prev
Next >



C++ Writing a file on Disk




In our last article, we have explained how to read an existing file on the disk. In this tutorial, we are going to explain how to write a file the content of an file. But before we write the content of a file on the disk, we must open it. For this, the C++ language provides us stream classes, using which we could perform file output/write operations..

In order to perform a file output/write 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 write the content of a file.








Writing a file


Let's look at the mode required to write a file.

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






  • Writing a file using fstream stream class


  • Let us see a program to write to a file on disk with already initialized values. This file will be created in the current directory(the same directory where we are going to store the upcoming C++ program.).

    //Creating a file for performing for output/write operation.
     
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
    
    //Using the ofstream class to create a file and to open an output stream to the file, to write content to it
    ofstream ofstream_ob("File1.txt", ios::out);
    
    int i = 10;
    
    //writing an int value to the file using ofstream object
    ofstream_ob<< i << "\n";
    
    
    char ch = 'a';
    
    //writing an char value to the file
    ofstream_ob<< ch << "\n";
    
    float f = 9.99f;
    
    //Writing a float value to the file
    ofstream_ob<< f << "\n";
    
    char arr[] = "Hello World";
    
    //writing a char array to the file
    ofstream_ob<<"Hello World!" << "\n";
    
    
    string str = "Keep smiling!";
    
    //writing a string to the file
    ofstream_ob<<"Keep Smiling!"<< "\n";
    
    
    //closing the output stream after completing the write operation on the file
    ofstream_ob.close();
    
    return 0;
    }

    Output


    Executing this program will create a file named File1.txt in the current directory, with its content-

    10
    a
    9.99
    Hello World!
    Keep Smiling!
    



    Advertisement




  • Writing a file by taking input from user at console


  • Let us see a code to write to a file on disk by taking input from the user at the console, This file will be created in the current directory(the same directory where we are going to store the upcoming C program.).

    //Creating a file for performing for output/write operation.
     
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
    
    //Using the ofstream class to create a file and to open an output stream to the file, to write content to it
    ofstream ofstream_ob("File2.txt", ios::out);
    
    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);
    cout<<arr;
    
    
    //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 : 10
    Enter a char value : a
    Enter a float value : 9.99
    Enter the content of char array : Hello World!
    Enter the content of a string : Keep Smiling


    Executing this program will create a file named File2.txt in the current directory, with its content taken from the input typed by the user at the console such as -

    File2.txt
    10
    a
    9.99
    Hello World!
    Keep Smiling


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




    Please share this article -





    < Prev
    Next >
    < C++ Reading a File
    C++ Append to a File >



    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