Advertisement
file-stream-object.open("filename", mode);
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. |
void put(char ch)
Advertisement
//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;
}
Hello World. We wish you best in everything. Never give up!
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement