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()
{
fstream file;
int a=5, b=10, c=15, x, y, z;
file.open("variables.txt", ios::out);
int arr[3]={a,b,c};
for(int i=0; i<3; i++){
file<<arr[i]<<endl;
}
fstream newFile;
newFile.open("variables.txt", ios::in);
int input[3];
for(int i=0; i<3; i++){
newFile>>input[i];
}
x = input[0];
y = input[1];
z= input[2];
cout<<"X is "<<x<<"\n Y is "<<y<<"\n Z is "<<z;
}
Comments
Leave a comment