Appending to a File and using Fstream class for Read and Write





This video tutorial explains how to append to a file in c++ programming and also explains how to open the file using fstream object for read and write.
You are gonna learn how to open a file for appending, which file opening mode to use to append to the file, how to open file for both read and write operation in detail with an example.

source code for this tutorial

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
fstream file("anil.txt",ios::in | ios::out | ios::app);
if(!file.is_open()){
    cout << "error while opening the file"<<endl;
}else{
cout <<"file opened succesfully"<<endl;
cout <<"writing to the file"<<endl;

file << "learning lad rocks"<<endl;

file.seekg(0);
cout <<"reading from the file"<<endl;
string line;
while(file.good()){
    getline(file,line);
    cout << line <<endl;

}


}
    return 0;
}

 
Back To Top