Advertisement
file-stream-object.open("filename", mode);
File Mode | Description |
---|---|
ios::app | This mode searches for a file and opens it in the 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
Greetings! Hope you are doing well.
//C++ 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;
//Creating and initializing a char array
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, by using the mode - ios::app
fstream_ob.open("File.txt", ios::app);
//Appending the char array 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;
}
Greetings! Hope you are doing well. Best wishes for all your endeavours!
Too much of work and no play made Jack a dull boy.
//C++ 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 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;
}
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
Too much of work and no play made Jack a dull boy.1000
B
15.4
Blue Sky
Always Shine
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement