C++ read and write file program (prototype)!

in programming •  7 years ago 

Program body

#include iostream
#include string
#include fstream
using namespace std;

int main (){
string io;
cout<<"if you wanna read the file than enter read"<<endl;
cout<<"if you wanna write to file than write down in source code and enter 'write' at runtime"<<endl;
cin>>io;
if(io=="read"){
ifstream file("kirito.txt");
if(!file.is_open()){
cout<<"An Error Occured"<<endl;
}else{
cout<<"Successfully opened file"<<endl;
string lines;
while(file.good()){
getline(file,lines);
cout<<lines<<endl;
}
}
}else if (io=="write"){
ofstream file1("kirito.txt");
if(!file1.is_open()){
cout<<"Error in opening file"<<endl;

}else {
cout<<"Successfully opened file"<<endl;
file1 <<"Lets share our knowledge with other steemians through LEARNANDGROW"<<endl;
file1 <<"Lets do it shall we"<<endl;
cout<<"successfully written you comments"<<endl;
}
}else {
cout<<"please check your spelling and it only supports read and write commands"<<endl;
}
return 0;
}

Working of program

first of all its a prototype program.
moving on first you gotta select which catagory you wanna choose "read" or "write"
za.jpg
where as if you choose the write option as it is prototype program you gotta write here in source code.
za1.jpg
You will see that if you did it right. YOu will get that in the file.
za2.jpg
If you wanna read so simply it will appear in execution window.
za3.jpg

I hope you guys liked it stay tuned for more.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!