I have written the following code in C++ which writes Student object to a file, then used seekp() to move the output pointer to a particular position where I wrote a Student object that replaced the previous object at that position, keeping others intact.
To get the same result, at which mode should I open the file where seekp() is done? (As commented)
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
class Student{
private:
int roll;
char name[30];
int marks;
public:
Student(int roll=0, char const nameData[]="", int marks=0){
this->roll=roll;
strcpy(name,nameData);
this->marks=marks;
}
void print(){
cout<<"Student Name : "<<name<<endl;
cout<<"Student Roll No : "<<roll<<endl;
cout<<"Student's Score : "<<marks<<endl;
}
};
int main(){
Student s1(25,"D Dhar",85);
Student s2(28, "A Sinha ",45);
Student s3(29, "B Sinha ",49);
Student s4(45, "C Sinha ",96);
Student s5(47, "D Sinha ",23);
s1.print();
s2.print();
s3.print();
s4.print();
s5.print();
cout<<endl;
fstream f;
f.open("abc.txt", ios::out);
f.write((char*)&s1,sizeof(s1));
f.write((char*)&s2,sizeof(s2));
f.write((char*)&s3,sizeof(s3));
f.write((char*)&s4,sizeof(s4));
f.write((char*)&s5,sizeof(s5));
f.close();
f.open("abc.txt", ios::in);
while(f){
Student s ;
f.read((char*)&s,sizeof(s));
s.print();
cout<<"Printed"<<endl<<endl;
}
f.close();
cout<<endl<<endl<<"***************"<<endl<<endl;
Student s,mystud;
f.open("abc.txt"); // what is the mode?
f.seekp(2*sizeof(s),ios::beg);
Student s_new(69,"Ramesh",69);
f.write((char*)&s_new,sizeof(s_new));
f.close();
cout<<endl<<endl<<"***************"<<endl<<endl;
f.open("abc.txt", ios::in);
while(f){
Student s ;
f.read((char*)&s,sizeof(s));
s.print();
cout<<"Printed"<<endl<<endl;
}
f.close();
mystud.print();
}
OUTPUT:
Student Name : D Dhar
Student Roll No : 25
Student's Score : 85
Student Name : A Sinha
Student Roll No : 28
Student's Score : 45
Student Name : B Sinha
Student Roll No : 29
Student's Score : 49
Student Name : C Sinha
Student Roll No : 45
Student's Score : 96
Student Name : D Sinha
Student Roll No : 47
Student's Score : 23
Student Name : D Dhar
Student Roll No : 25
Student's Score : 85
Printed
Student Name : A Sinha
Student Roll No : 28
Student's Score : 45
Printed
Student Name : B Sinha
Student Roll No : 29
Student's Score : 49
Printed
Student Name : C Sinha
Student Roll No : 45
Student's Score : 96
Printed
Student Name : D Sinha
Student Roll No : 47
Student's Score : 23
Printed
Student Name :
Student Roll No : 0
Student's Score : 0
Printed
***************
***************
Student Name : D Dhar
Student Roll No : 25
Student's Score : 85
Printed
Student Name : A Sinha
Student Roll No : 28
Student's Score : 45
Printed
Student Name : Ramesh
Student Roll No : 69
Student's Score : 69
Printed
Student Name : C Sinha
Student Roll No : 45
Student's Score : 96
Printed
Student Name : D Sinha
Student Roll No : 47
Student's Score : 23
Printed
Student Name :
Student Roll No : 0
Student's Score : 0
Printed
Student Name :
Student Roll No : 0
Student's Score : 0