Click here to Skip to main content
15,903,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Don't know the meaning of this code in bold, please help. I do know about the file handling but not about the sizeof() function etc. please help!
C++
void function()
{ofstream o;
	char ch;
o.open("abc.txt",ios::out|ios::app);
	do
	{
		system ("cls");
		bk.create_book();   //create_book is a function in the class by the name of "book"//
		o.write((char*)&bk,sizeof(book));     //bk is an object of the class "book"//
		cout<<"\n\nDo you want to add more record..(y/n?)";
		cin>>ch;
	}while(ch=='y'||ch=='Y');
	o.close();
}
Posted
Updated 10-May-12 8:20am
v3

The short answer is something you shouldn't do in C++ unless you really know what you're doing, and even then there's usually a better way.

The long answer is that it's writing the block of memory containing bk to the file "abc.txt." Which hopefully isn't something you're going to want to edit with notepad as it sure as hell won't be a text file after a lump of any old rubbish gets written in there.

Cheers,

Ash

PS: Really, don't do it...

PPS: No, take your hands away from the keyboard unless you're going to delete that lump of code...
 
Share this answer
 
Comments
ShayanTanwir 10-May-12 14:18pm    
hehe thanks mate, the problem is that this specific code o.write function and sizeof() are unknown to me. I know about the file handling though.
The bold line is doing 'poor-man' serialization. the sizeof operator is (surprisingly!) documented, see, for instance MSDN[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-May-12 16:26pm    
Right, a 5.
--SA
sizeof() operator returns the size in bytes of the object you pass as a parameter.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900