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
#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();
}
}
Comments
Leave a comment