Assuming we have following variables and values:
int a=5, b=10, c=15, x, y, z;
Write the values of a, b and c variables in file by using formatted file I/O and then read data accordingly from file and assign it to x, y and z variables. Just create objects of relevant classes, there is no need to write entire main function.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a=5, b=10, c=15, x, y, z;
ofstream out ("text.txt");
out<<"\na= "<<a;
out<<"\nb= "<<b;
out<<"\nc= "<<c;
out.close();
ifstream in("text.txt");
string n;
while (getline (in, n)) {
// Output the text from the file
cout<<n;
in.close();
}
return 0;
}
Comments