Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sizeof and write question

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US

Please explain what sizeof operator is doing in this piece of code?
Also if someone could please explain what is going on (in detail) with the
whole line:
[tt] outfile.write((char *) &s1, sizeof(s1));[/tt]

Here is how it is used:

[tt]
{
s1.get_data();
outfile.write((char *) &s1, sizeof(s1));
cout <<&quot;Enter q to quit or y to continue : &quot;;
}
[/tt]
 
what the sizeof returns is the sizeof the structure (or class but most likely a structure)

If the structure contains no pointers the exact data in 8 bit representation will be printed out to the file. If however there are pointers to arrays (etc) then the output will be printing out memory addresses instead.

What can then be done in the future is a file can be opened and read into a pointer to the structure cast as a character array and retrieved as it was when printed out.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top