. 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.
/*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;
}
Comments
Leave a comment