简体   繁体   中英

Saving and Reading struct array with binary file in C language

There are some problems with my reading function.

My reading function is:

int readFileToPic(FILE* f, pic* picture_Reccord, int picNumber){

 int count = 0;

 f = fopen("Record.dat", "rb");

 if (f == NULL){
    printf("\n Unable to open file!\n");
 }
 else{
    count= fread(&picture_Reccord, sizeof(picture_Reccord), Maximun_Picture,    f);
    fclose(f);
 }
 if (count <=0 && count >= Maximun_Picture)
    return -1;

  //breaking the programe
  //picture_Reccord[count].fileName[0] = '\n';

 return count;
  }

This is how I call it

case 3:
    printf("Read picture records from disk\n");
    count= readFileToPic(file, pictureRecord, picNumber);
    printf("\n\nRead %d photos\n", count);
    //testing
    printf("\n%d\n", pictureRecord[0].location);
    break;

It prints "Read 4 photos" every time and for grabage in the testing part

This is my saving function in case this is where the problems are

void savePic(FILE* f, pic picture_Record){

f= fopen("Record.dat","wb");
if (f == NULL)
{
    printf("\nError! Not able to save!\n");
}

fwrite(&picture_Record, sizeof(picture_Record), 1, f);
fclose(f);

printf("One Pic Saved\n");

}

This is my updated struct thanks to @ Jonathan Leffler

typedef struct picture_Data
{
char fileName[Input_Length_fileName];
char description[Input_Length_description];
char location[Input_Length_fileName];
int peopleCount;
}pic;

Can anybody help me and tell me where the problems are ?

When your structure contains pointers, it cannot be 'serialized' sensibly. That is, it cannot be written using any of the functions like fwrite() .

Why not, you ask? Good question!

Think of it this way: if you have a structure containing a pointer to a string (a name, for example), then the data in the string is usually not contiguous with the structure, and certainly its size isn't included in the size of the structure.

By contrast, if the structure contains a (fixed size) array of characters, then you can write the whole structure because the data is contiguous with the rest of the structure, and is counted in the size of the array.

Flexible array members require a little bit of care, but can be written to disk provided the main structure records how big the flexible array is. You can write in one operation, but you need to read in two. The first read reads the inflexible part of the structure; one of the data items in that tells you how much space to allocate and how much data to read.

This line

count= fread(&picture_Reccord, sizeof(picture_Reccord), Maximun_Picture, f);

is definitely wrong. It needs to be

count= fread(picture_Reccord, sizeof(pic), Maximun_Picture, f);

Take a simpler example to see the diffeence. Say you have:

int i;
int* ip = &i;

When you want to read an integer from a file into i , you need to use

fread(&i, sizeof(int), 1, f);

or

fread(p, sizeof(int), 1, f);

but not

fread(&p, sizeof(p), 1, f);

Also, I don't see where you have allocated memory to hold Maximun_Picture pic s. If you want read an array of 10 integers from a file, you have to use something like:

int* ip = malloc(10*sizeof(int));
fread(ip, sizeof(int), 10, f);

Without allocating memory for Maximun_Picture pic s, you will be using unauthorized memory, which will result in undefined behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM