Answer to Question #213900 in C++ for ayesha manzoor

Question #213900

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:21-0400
#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;
}

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