Advertisement



< Prev
Next >



C++ File Output put() Function






In our last article, we have explained how to use a very important file input get() function, which is used to read a character from the file. In this tutorial, we are going to explain how to use a very important file output 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 a file. For this, the C++ language provides us stream classes which are used to perform file output/write operation.




File Stream Classes


C++ provides us a few file stream classes which allow us perform file output/write operation by using the put() function. These file stream classes are:
These two file stream classes provides us a function named open(), using which we could provide the location of a file stored on disk and could also specify a mode in which we want to open this file





Let's see a specific file mode to use the put() function, to perform output/write operation on a file.




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


Let's take a look at a specfic mode which is required to write the content of a file by using the put() function.

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

Through this mode, we could use the put() output function to write to a file on disk.






  • The syntax of put() file function


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

     void put(char ch)



    Advertisement




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


  • Let's say that we have to create a new file on disk named File2.txt, and we will also write its content 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).


    //C++ Writing data to a file using put() function and ios::out mode
     
     
    #include<iostream>
    #include<fstream>
    #include<cstring>
    
    using namespace std;
    
    int main()
    {
    
    	//Creating an output stream to write data to a file
    	ofstream ofstream_ob;
    
    
    	//Opens/creates a file named File2.txt 
    	ofstream_ob.open("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 file, by using put() function
    	}
    
    	//Closing the output stream 
    	ofstream_ob.close();
    
    	return 0;
    }

    Output


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

    File2.txt
    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 >



    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

    C#, PYTHON and DJANGO


    Advertisement