Answer to Question #327050 in C++ for Pragnan

Question #327050

Write a program to create a class students having name, id, age. Create a file student.txt and open in append mode. Write the detail of 3 students into a file and read the data from the file and display on screen. Also copy content of this file into another file

1
Expert's answer
2022-04-11T07:45:48-0400
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


class Student
{
	string name;
	int ID;
	int age;
public:
	Student(string _name, int _ID, int _age)
	:name(_name),ID(_ID),age(_age){}
	string GetName() { return name; }
	int GetID() { return ID; }
	int GetAge() { return age; }
};


int main()
{
	Student arr[3] = { Student("Jack",12345,24),Student("Mary",98763,21),
					Student("John",24934,20)};
	ofstream of;
	of.open("student.txt",ofstream::out||ofstream::app);
	if (!of.is_open())
		cout << "File isn`t opened!";
	else
	{
		for (int i = 0; i < 3; i++)
		{
			of << arr[i].GetName()<<" " << arr[i].GetID()<<" "
				<< arr[i].GetAge() << endl;
		}
		of.close();
	}
	ifstream ifs("student.txt");
	ofstream ofnew;
	ofnew.open("studentCopy.txt", ofstream::out || ofstream::app);
	if (!ifs.is_open())
		cout << "File isn`t opened!";
	else
	{
		string line;
		while (getline(ifs, line, '\n'))
		{
			cout << line<<endl;
			ofnew << line<<endl;
		}
		ofnew.close();
		ifs.close();
	}
}

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