< Prev
Next >

C++ File Output put() Function

In our last article, we have explained how to use a very important file input/read function - get() function, which is used to read a character from the file. In this tutorial, we are going to explain how to use the a very important file output/write function -

Before we show you how to use this file output function to write to a file on the disk, we must show you how to open the file. For this, the C++ language provides us stream classes used to perform file output/write operation.

File Stream Classes

In order to perform a file output/write operation using the put() function, 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 see the file mode that we require to use, in order to use the put() function to perform output/write operation on a file.

Mode to perform the file input operation using put() function.

Let's look at the combination of mode required to write the content of a file using the put() function.

File Mode Description
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. Using this mode, we could use the put() output function to write to a file on disk.

  • The syntax of put() file function

  • The put function allows us to write one character at a time to a file on disk. Let's see the syntax of put() file output function, before we use it to write content of a file.

     void put(char ch)


  • Using the put() function to perform the file output operation.

  • Let's say that we to create a new file on disk, named File2.txt and we will also write the content of this file by using the put() file output function. This file will be created in the current directory(the same directory where we are going to store the upcoming C++ program).

    //Writing data to a file using ofstream class and mode ios::out
    using namespace std;
    int main()
    //Creating an output stream to write data to a file
    ofstream ofstream_ob;
    //Opening a file named File2.txt to modify the old content"File2.txt", ios::out);
    char arr[100] = "Hello World. We wish you best in everything. Never give up!";
    int length = strlen(arr);
    char ch;
    //Reading the char array i.e. a character at a time and writing it to the file
    for(int i=0; i<length; i++)
    ch = arr[i];
    ofstream_ob.put(ch);	//Writing a character to a file using put() function
    //Closing the output stream 
    return 0;


    Executing this program will create a new file named File2.txt in the current directory and we have even written the content of this file using put function. The content of the file looks like this :

    Hello World. We wish you best in everything. Never give up!

    Please share this article -

    < Prev
    Next >
    < File Input Function - get()
    File Input Pointer - get >


    Please Subscribe

    Please subscribe to our social media channels for daily updates.

    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



    Please check our latest addition