Louis
Louis

Reputation: 31

fwrite doens't save full array in C

I have an array of size 668x493 which i want to save. So i am doing the following.

data : is a pointer to an array that holds the values.

long lSize;
FILE* image_save;
image_save=fopen("image_save_file.bin","w+b");
fwrite(data,1,329324,image_save);

However, when i read back this array:

char* check_image;

p1File=fopen("image_save_file.bin","r+b");

fseek (p1File , 0 , SEEK_END);
lSize = ftell (p1File);
fseek (p1File , 0 , SEEK_SET);

when i check lSize, i see it 327680 ???

So of course when i do fread i get only 327680 values !

Kindly asking, can you pinpoint my mistake ?

Upvotes: 0

Views: 1054

Answers (3)

jpm
jpm

Reputation: 3155

fwrite returns an int indicating the actual number of bytes written. Double check to make sure this differs from expected (it almost certainly does). Then, you can use perror to print out the error that's occurring.

Upvotes: 2

Steve Townsend
Steve Townsend

Reputation: 54138

Interestingly, 327680 is an exact multiple of 4096 (80 * 4096).

Are you flushing/closing the output file before you read the data back in?

Upvotes: 10

Milan
Milan

Reputation: 15849

The fwrite() function is buffered. Try flushing the data on the file stream and try again.

Upvotes: 5

Related Questions