help with struct and i/o please

Started by
12 comments, last by angelxe1 19 years, 4 months ago
Don't apologise about asking. You don't learn if you don't ask. As long as people show that they're willing to have a go at solving their problems and not just sponge answers then I'm more than willing to help.

I suspect you're getting to "can't see the wood for the trees" mode. This often happens when you stare at a bug for too long. So for now, completely forget about your code and I'll quickly go through some basics which you probably know but are beginning to get confused about because you've been bashing your head against them so long.
struct SomeStruct{	int memberVariable1;	char memberVariable2[15];};

This is the definition for a struct. All it does is tell the compiler what that structure looks like. In this case you're telling the compiler: "I have a structure called SomeStruct which I'll be using later. SomeStruct objects consist of an integer and an array of 15 characters".
The size of SomeStruct is the amount of space (in bytes) the compiler would allocate for a single instance of SomeStruct. In this case the size of SomeStruct is 20 bytes. That's 4 for the int, 15 for the character array and an extra 1 to pad it to a multiple of 4 because that's how computers work best.
SomeStruct aStruct;

This is the definition for an instance of SomeStruct. Here the compiler actually allocates memory for a SomeStruct. In this case you're telling the compiler: "You know that SomeStruct structure I told you about earlier? Well, I want one of them now called aStruct".
Since the size of SomeStruct is the amount of space (in bytes) the compiler would allocate for a single instance of SomeStruct it follows that the sizeof aStruct (an instance of SomeStruct) is exactly the same.
SomeStruct someStructs[10];

This is the definition for an array of SomeStructs. Here the compiler must allocate memory for 10 SomeStructs. In this case you're telling the compiler: "You know that SomeStruct structure I told you about earlier? Well, I want ten of them now in a row".
This is roughtly equivalent to doing:
SomeStruct aStruct1;SomeStruct aStruct2;SomeStruct aStruct3;...SomeStruct aStruct10;

Since someStructs contains 10 SomeStruct objects is follows that the size of someStructs is 10 times the size of SomeStruct, so the size of someStructs in this case is 200 bytes.
Now lets apply this to your teachers example:
struct student{char name[25];int grade;}; // definition of the class - takes up no memory, just tells the compiler what a student object will look likeint main(){student s; // definition for a student object.  Here the compiler  allocates memory for the object - probably 32 bytes with paddingofstream fout;fout.open("test.dat", ios::binary);// code to enter names and grades accordingly;fout.write((char*)&s, sizeof(student)); // write the student object to the file.  We have only one student object, which is 32 bytes in size, so we want to write 32 bytes to the file.  sizeof(student) gives that 32 bytes.  It is exactly the size of the object we are writing.  sizeof(s) would also work here since the size of a class is the same as the size of a single instance of that class// more code}


Enigma
Advertisement
Quote:Original post by angelxe1
but all i see when i open the file is a bunch of binary then lena then more binary. (why is lena not in binary?)


It is. "Lena" 'in binary' still looks like "Lena".
ohhhh...i get it now.

Washu - Zahlman - thank you for your help. i will consider writing the code again.

Enigma - thanks a million times for helping me through this. by no means did i ever expect you to just tell me the answer. i do want to learn how to do it myself, and i'm glad you think the same way.

wish i had found this board earlier in the school year.

= )
i'm working on another program with struct i/o files...and stuck once again. this time with updating one file with another file. so i was wondering are there any tutorials that you guys recomend on this topic? (The ones i have found so far are just not that much help)

This topic is closed to new replies.

Advertisement