0

I am not able to understand the fread fwrite behavior of the following code snippet, exemplified by the code is straightforward:

#include <stdio.h>
int main(void)
{
    FILE *fp;
    int   arr[10] = {1,2,3,4,5,6,7,8,9,10};
    int   temp[100] = {0};
    int   i;

    fp = fopen("testdata.bin","wb");
    if( fp!= NULL ) {
        fwrite( arr,sizeof(int), 10, fp);
        fclose(fp);
    }
    fp = fopen("testdata.bin","rb");
    if( fp!= NULL ) {
        fread( temp,sizeof(int), 10, fp);
        fclose(fp);
    }
    for(i=0;i<100;i++)
        printf("%#x,",temp[i]);
    printf("\b \n");
    return 0;
}

The output on stdout is:

0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

which makes sense. However, when I open the testdata.bin file, I see only two bytes for value (int) where I expect 4 bytes as size of int is 4 on my machine.

Here is the content of testdata.bin:

0x00000001: 01 00 02 00 03 00 04 00 05 00 06 00 07 00 08 00
0x00000010: 09 00 0a 00 

I would have expected

0x00000001: 01 00 00 00 02 00 00 00 03 00 00 00 ...

Any ideas?

22
  • 2
    You don't report in your code whether the program was able to open the file for writing. You simply don't write in it if the fopen() fails. Check the return from fopen(), and for debugging, report on its status. Commented Aug 17, 2017 at 2:24
  • 1
    I think you need to print out the size of an int on your machine. It looks to me like you're managing to use 2-byte int. The output and input are agreeing. Use printf("sizeof(int) = %zu\n", sizeof(int)); to ensure that what you think is happening actually is happening. Commented Aug 17, 2017 at 2:29
  • 1
    @Vikas, what does printf("%d\n",sizeof(int)) give you? Commented Aug 17, 2017 at 2:34
  • 1
    Like I told you — print the value of sizeof(int) in the output. On my machine, I get 4 and output similar to what you expect. Your code is self-coherent, but the file is half the size you expected; that suggests strongly that your data type is half the size you expected. Commented Aug 17, 2017 at 2:35
  • 2
    Okay, put system("cd") or system("pwd") at the start of your code to make sure you're in the directory you think you're in. If you're in an IDE, you may be looking at the wrong (e.g., earlier version of the) file. Commented Aug 17, 2017 at 2:36

1 Answer 1

1

I think fwrite is working fine. Change the declaration of temp so the type is an array of one-byte unsigned characters:

unsigned char temp [100] = {0} ;

The current version of the code displays a four-byte integer each time in the print statement. This will confirm that the contents of the file are as you expect. On my machine:

0x1,0,0,0,0x2,0,0,0,0x3,0,0,0,0x4,0,0,0,0x5,0,0,0,0x6,0,0,0,0x7,0,0,0,0x8,0,0,0,0x9,0,0,0,0xa,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Sign up to request clarification or add additional context in comments.

1 Comment

I tried the suggested change and I do see the same output as shown above! Which means the code is right.

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.