Answer to Question #213868 in C++ for Adban

Question #213868

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.




1
Expert's answer
2021-07-05T07:04:25-0400
#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;
}

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