Answer to Question #180188 in C++ for vivek

Question #180188

There are following two major issues associated with cpp programs:

  • When a program is terminated, the entire data is lost.
  • If you have to enter a large number of data, it will take a lot of time to enter them all in
  • the different programs.

Suggest a solution and elaborate the same with the help of suitable examples. 



1
Expert's answer
2021-04-12T04:50:41-0400
/*The problem of saving data can be solved by saving data to a file, and in case of restarting the program, we can easily get data from the file.*/
 
#include <iostream>
#include <fstream>
using namespace std;
 
int main()
{
	int A[10];
	for (int i = 0; i < 10; i++)
	{
		A[i] = i;
	}
	ofstream fout;
	fout.open("Test.txt");
	for (int i = 0; i < 10; i++)
	{
		if (i == 9)
		{
			fout << A[i];
			break;
		}
		fout << A[i] << " ";
	}
	int B;
	fout.close();
	ifstream fin;
	fin.open("Test.txt");
	cout << "Data from file: ";
	while (!fin.eof())
	{
		fin >> B;
		cout << B << " ";
	}
	cout << endl;
}

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