0

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 ?

9
  • Turn on your compiler warnings. Unless Maximum_Picture is negative, there's no way on Earth count <=0 && count >= Maximun_Picture could possibly be true ever. Commented May 18, 2014 at 6:16
  • Also, &picture_Reccord in the first function should be picture_Reccord, because it's already a pointer. Commented May 18, 2014 at 6:17
  • @user3477950 Yes it should be || instead of &&, thanks Commented May 18, 2014 at 6:18
  • 2
    It would be helpful to see the definition of the structure. If it contains pointers, it can't readily be written to disk with fwrite() and read back with fread(). Commented May 18, 2014 at 6:22
  • 1
    Your problem is that your structure contains pointers and consequently cannot sensibly be written to disk with fwrite(). Commented May 18, 2014 at 6:26

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 pics. 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 pics, you will be using unauthorized memory, which will result in undefined behavior.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.