Alex
Alex

Reputation: 49

Read/Write int Binary File in C error

I am facing an error reading/writing to a binary file in C. The issue is: - I write an int (e.g. 2) to a file using fwrite - I try read this int from a file using fread - When I read it, it doesn't come out as 2, but rather some long number e.g. 967899906 or 2071332354 or 1030372866 (always different).

I write to a binary file in C like this:

FILE * writeFile = fopen("test.bin","wb");

//test int vars
int test1 = 1;
int test2 = 2;
int test3 = 3;

//writing some ints to binary file
fwrite(&test1,1,1, writeFile);
fwrite(&test2,1,1, writeFile);
fwrite(&test3,1,1, writeFile);

fclose(writeFile);

I then read from the file like this:

FILE *readFile = fopen("test.bin","rb");
int data;
while(fread(&data,1,1, readFile)==1){
   printf("%d", data); //here it will output some random number
}
fclose(readFile);

Upvotes: 1

Views: 1623

Answers (2)

JeremyP
JeremyP

Reputation: 86651

LPs is right about that if you have a range of 0 <= x <= 255, you should consider uint8_t.

However, if you do want to use an int, the reason you are probably getting garbage is because

fread(&data,1,1, readFile)==1)

reads a single byte of data and puts it at the location specified by the pointer. In your case, this will be the lowest byte of data. Ints are typically 4 bytes long so the other 3 bytes will be unchanged. What are the other three bytes? Well, in your case, they could be anything because data is uninitialised on the stack.

Upvotes: 0

LPs
LPs

Reputation: 16223

fwrite wants, as second parameter, size of data to write, so in your case sizeof(int)

Same thing for fread

#include <stdio.h>

int main(void)
{
     FILE * writeFile = fopen("test.bin","wb");

     if (writeFile != NULL)
     {
         //test int vars
         int test1 = 1;
         int test2 = 2;
         int test3 = 3;

         //writing some ints to binary file
         fwrite(&test1,sizeof(test1),1, writeFile);
         fwrite(&test2,sizeof(test2),1, writeFile);
         fwrite(&test3,sizeof(test3),1, writeFile);

         fclose(writeFile);
     }
     else
     {
         perror("Error opening file to write: ");
         return 1;
     }


     FILE *readFile = fopen("test.bin","rb");
     if (readFile != NULL)
     {
         int data;
         while(fread(&data,sizeof(data),1, readFile)==1){
            printf("%d\n", data);
         }
         fclose(readFile);
     }
     else
     {
         perror("Error opening file to read: ");
         return 1;
     }

    return 0;
}

Hint: always test if fopen succeed.

EDIT

To use 1 byte variable, as commented, just use uint8_t variables

#include <stdio.h>
#include <stdint.h>

int main(void)
{
     FILE * writeFile = fopen("test.bin","wb");

     if (writeFile != NULL)
     {
         //test int vars
         uint8_t test1 = 1;
         uint8_t test2 = 2;
         uint8_t test3 = 3;

         //writing some ints to binary file
         fwrite(&test1,sizeof(test1),1, writeFile);
         fwrite(&test2,sizeof(test2),1, writeFile);
         fwrite(&test3,sizeof(test3),1, writeFile);

         fclose(writeFile);
     }
     else
     {
         perror("Error opening file to write: ");
         return 1;
     }


     FILE *readFile = fopen("test.bin","rb");
     if (readFile != NULL)
     {
         uint8_t data;
         while(fread(&data,sizeof(data),1, readFile)==1){
            printf("%d\n", data);
         }
         fclose(readFile);
     }
     else
     {
         perror("Error opening file to read: ");
         return 1;
     }

    return 0;
}

Upvotes: 1

Related Questions