0

This is a homework project where user interacts with a database of people that have data such as a number, street name etc. the which data resides in a text file.

Some menu options require the program to read or manipulate that data, for example a Display option. I've done this by opening an fstream and putting the data into a temporary array that has for data type the pre-declared struct resident.

//reopen file
fstream listeResidents1;
listeResidents1.open("listeResidents.dat",ios::out|ios::in|ios::binary);

//Create a temporary array of objects resident for manipulation
resident tabRes1[nombreResid-1];

//Insertion des objets resident du registre dans le tableau temporaire
for (int h = 0;h < nombreResid;h++)
{
    listeResidents1.seekg((h)*sizeof(resident));
    listeResidents1.read(reinterpret_cast<char*>(&tabRes1[h]),sizeof(resident));
}

listeResidents1.close();

My Display() function does this^^^, then uses a for loop to go through each object in the array and display all of their attributes. However the very last entry has its penultimate string missing some characters and the last variable, a float, set to 0 (which means it was also truncated) and I can't figure out why. Does anyone have any idea why this happens?

For a visual example, last entry was successfully written to file with these attributes:

int numero: 4
[the other,correctly displayed attributes here]
char rue[30]: queen
float superf: 80

when displayed the last entry would display

numero: 4
[the other,correctly displayed attributes here]
rue: que
superf: 0

If I add another entry, it is the one truncated while 4 is now displayed correctly...

Edit: here is the struct.

struct resident
    {
        int numero;
        char nom[30];
        char prenom[30];
        int numciv;
        char rue[30];
        float superf;
    };
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
blue0
  • 23
  • 1
  • 5

1 Answers1

0

You're reading nombreResid items into memory, but your buffer resident tabRes1[nombreResid-1] has room for only nombreResid-1 items.

Chris Culter
  • 4,470
  • 2
  • 15
  • 30
  • The for loop stops after h is < than nombreResid, isn't that the same as h <= nombreResid -1? – blue0 Aug 10 '14 at 05:25
  • @blue0 Right, but there's another consideration: In C++, arrays are indexed starting from 0. If your array is `resident tabRes1[nombreResid-1]`, then the first item in the array is `tabRes1[0]` and the last item is `tabRes1[nombreResid-2]`. – Chris Culter Aug 10 '14 at 05:27
  • Oh god, I forgot. So if I declare an array[10], it will have entries 0-9. I feel so stupid, I'll try after changing this, thank you very much. I wonder why the rest of the data of the last entry was still there though, but I suppose that has to do with subtleties about the binary code I still don't understand. – blue0 Aug 10 '14 at 05:31
  • Well, after you write to memory just off the end of `tabRes1`, some other operation might be overwriting the same area in memory, possibly with a bunch of zeroes. The area just after `tabRes1` might escape getting clobbered, so it happens to be in the same state when you get around to reading it. – Chris Culter Aug 10 '14 at 05:49