Answer to Question #218866 in C++ for qwe

Question #218866

Write a program to writing in to a text file, read from a text file and display in c++


1
Expert's answer
2021-07-26T23:02:02-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS