Write a program to writing in to a text file, read from a text file and display in c++
#include <iostream>//input output stream
#include <fstream>//file sream
#include <stdlib.h>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
bool quit = false;
while (!quit)
{
cout << "Main menu\n";
cout << "\t1 -Write in to a text file\n";
cout << "\t2 -Read file and display content\n ";
cout << "\t3 -Quit\n";
int cmd;
cin >> cmd;
switch (cmd)
{
case 1:
{
cout << "Please enter name file(with format \".txt\"):";
string fname;
cin >> fname;
ofstream of(fname);//create new file
string txt;
cout << "Please enter text: ";
cin.get();
getline(cin, txt);
of << txt;
of.close();
cout << "Done!!!\n";
break;
}
case 2:
{
cout << "Please enter name file(with format \".txt\"):";
string fname;
cin >> fname;
ifstream f(fname);//create new file
string txt;
getline(f, txt);
cout << "=========================CONTENT==============================\n";
cout << txt << endl;
f.close();
break;
}
case 3:
{
cout << "Quit\n";
quit = true;
break;
}
default:
break;
}
}
return 0;
}
Comments
Leave a comment