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;
//Implement class FILE
class File
{
private:
string fname;//File name
int a, b, c;//variables constants
int x, y, z;
public:
File(string _fname)
{
a = 5;
b = 10;
c = 15;
this->fname = _fname;
ofstream of(fname);//Open file
of << a << b << c;//Write date
of.close();
}
void ReadXYZ()
{
ifstream in(this->fname);
in >> this->x >> this->y >> this->z;
in.close();
}
int getX()
{
return this->x;
}
int getY()
{
return this->y;
}
int getZ()
{
this->z;
}
};
int main()
{
//Before you may be create text file "input.txt" in write 1 3 5
File fl("input.txt");
cout << fl.getX << " " << fl.getY() << " " << fl.getZ() << endl;
return 0;
}
Comments
Leave a comment