I have posted the update member function as many the 12th students can copy my project. I want to open the binary file compare the roll numbers with the inputted roll if it matches the object of the file should be overwritten with a new object i.e *X.

void Report_card :: Update()
{
	cleardevice();
	settextstyle(4,HORIZ_DIR,3);
	setbkcolor(15);
	setcolor(BLUE);
	one_by_one(50,2,".....|| Update Records ||.....",3,100);
	settextstyle(0,HORIZ_DIR,1);

	int Ro;
	long file_pos;
	Report_card T,*X;

	OXY(5,70,"Enter the roll number of the student whose record is to be edited :");
	Ro = Inp_integer(5,90);  // graphical input
	fstream file;
	file.open("report.txt",ios::in|ios::out|ios::ate);
	if(!file)
	{
		OXY(5,110," Error in opening the file ! ");
		getch();
	}

	while(file.read((char*)&T,sizeof(T)))
	{
	       file.read((char*)&T,sizeof(T));
	       if (Ro == T.roll)
	       {
		    //OXY is outtextxy's name changed using a macro.

                    OXY(1,120," Enter the name of the student :    ");
		    Inp_string(500,120,X->name);

		    OXY(1,140," Enter the marks in Physics :       ");
		    X->mark[0] = Inp_integer(500,140);

		    OXY(1,160," Enter the marks in Chemistry :     ");
		    X->mark[1] = Inp_integer(500,160);

		    OXY(1,180," Enter the marks in Maths :         ");
		    X->mark[2] = Inp_integer(500,180);

		    OXY(1,200," Enter the marks in English :       ");
		    X->mark[3] = Inp_integer(500,200);

		    OXY(1,220," Enter the marks in C++ :           ");
		    X->mark[4] = Inp_integer(500,220);

		   file.write((char*)&(*X),sizeof(*X));
		   getch();
		   file.close();
		   break;
	       }
	       else
	       {
		   OXY(5,110,"Roll not found !");
		   delay(1000);
	       }

	}
}

the program just exits after inputting Ro. :(

Recommended Answers

All 6 Replies

What does
break;
do?

break; gets out the loop once a data is updated.

> Ro = Inp_integer(5,90); // graphical input
You mean it fails here?

> Inp_string(500,120,X->name);
Or perhaps here, since X isn't pointing anywhere useful.

no the graphical input is working perfectly fine.
The public data members of the class are name,roll,mark[5].

what I am trying to do is to read data from the file report.txt into
a temporary object T, if T.roll is = Ro, it should overwrite that data contained in object *X in the file.

What makes you think
Inp_string(500,120,X->name);
is in any way valid code?

In fact, why have a *X anyway?

Why not
Inp_string(500,120,T.name);

I tried that too, the problem them occurns that the data is written to the end of the file but I want the data to be overwritten at at that place in the file. I even tried seekg and tellg but even that dosen't seem to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.