when reading data from a binary file into a structure, where does the data go?

Well for school I have a programming project to do. The program has to read in data from a binary file and then output it to look like this:

1
2
3
4
              Canada      28      28      28      29      29      29
       United States     250     253     255     258     261     263
           Australia      17      17      17      18      18      18


I created a structure called Emp that I have to read the contents of the binary file into:
1
2
3
4
5
struct Emp
{
    char country[20];
    int population;
};

So now I have to read in the data from the binary file into an array of Emp's.
 
Emp rec[3];


So, I wrote a for loop to try to accomplish this:
1
2
for(int i=0; i < 3; i++)
ifile.read(reinterpret_cast<char *>(&rec[i]), sizeof(rec[0]));



ifile is the file that I opened. So now my problem is, I don't understand how to output the information. Where did it go? I sent it to the rec Emp but I don't know where the information is at or how to access it?

Any ideas to help me understand this would be greatly appreciated.

Thanks a lot!
So you know how to write structures/classes but not how to access their members?

Mmh... Yeah. Don't do that. Directly writing data to a class' memory is generally a bad idea.

Assuming that did work, you would access the data with rec[i].country, or rec[i].population.

By the way, &rec[i] is the same as &*(rec+i), which is the same as rec+i.
Last edited on
Google around "c++ serialization" for some good reading.

For each object, you should have a couple of functions: one to output it and one to input it.

Before I make any further suggestions, have you learned about std::vectors?

[edit] Or linked lists?
Last edited on
What I am confused about is how the read statement knows what part of the binary file to each member statement. How can I control what part of the file gets read to what? I just don't understand how it knows where to put everything. I actually did make a mistake though, I should have had the population member be an int array of 6. So now I have

1
2
3
4
5
struct Emp
{
    char country[20];
    int population[6];
};


And after reading in the data from the binary file into the rec Emp, I tried to output the contents like this:

1
2
3
4
5
6
    for(int i=0; i < 3; i++)
    {
        cout << rec[i].country << endl;
        for(int q=0; q < 6; q++)
            cout << rec[i].population[q] << endl;
    }


But that didn't output the correct information because the read statement must not have correctly assigned the data into each member of the rec Emp.

Please help me understand.
how the read statement knows what part of the binary file to each member
It doesn't.
What you need to understand is that a class is merely a bunch of variables bound together. In memory, your class might actually look like this*:
[country (20 bytes)] [population[0] (4 bytes)] [population[1] (4 bytes)] ... [population[5] (4 bytes)]

*...Or not. It depends on the compiler and the platform.

When you pass a pointer to an Emp casted as a pointer to char to read(), you're saying "read the contents of the file into here as though it was an array of chars, regardless of what actually was there".

Like I said before, this is a bad idea. This logic depends on the data having the same shape in the file as it would have in memory.
Suppose instead of the above scheme, the compiler decided for whatever reason to put 1 padding byte after country:
[country (20 bytes)] [padding (1 byte)] [population[0] (4 bytes)] [population[1] (4 bytes)] ... [population[5] (4 bytes)]
The code no longer works, because now read() is writing one of the bytes to a location that cannot be accessed as a member (without overflowing country).
An even more common occurrence is the file having a different endianness (http://en.wikipedia.org/wiki/Endianness) than the platform, which also results in corrupt data.
Last edited on
Thanks for all the help helios. I now understand what you're saying.

Anyways, I really need to finish this programming project though and am still not very close to being finished. I don't know if you'll have time to help me with this, but if you could it would be so appreciated.

The assignment instructions are on my instructors website on this page:

http://www.klickfamily.com/david/school/cis250/asst02sp09.html

If you could just take a look at the instructions and just explain to me a little on how I should go about it with like a few code examples or something it would really help me out.

Thank you so much. I wish I was as knowledgeable as you on the subject of C++ so I could get this assignment done myself :/
Last edited on
Topic archived. No new replies allowed.