cfilefwrite

Output not as expected in fwrite


I am learning fread and fwrite of c and made a basic code to write a structure using fwrite in a file . Output was there on the

#include <stdio.h>
int main()
{
    FILE *f;
    int i,q=0;
    typedef struct {
           int a;
           char ab[10];
    }b;
    b var[2];
    f=fopen("new.c","w");
    printf("Enter values in structure\n");
    for(i=0 ; i<2 ; i++)
    {
        scanf("%d",&var[i].a);
        scanf("%s",var[i].ab);
    }
    fwrite(var,sizeof(var),1,f);

    fclose(f);
    return 0;
}

The output was not smooth as it contained weird characters inside the file.

I opened the file in binary mode too but in vain. Is this some kind of buffer problem?


Solution

  • The "weird" characters inside your file are probably the bytes of the binary integers you're writing out. fwrite is writing the bits of var directly to the file, not converting that into a human readable format. If you want that, use fprintf instead.

    Here's an example directly from your code above:

    $ ./example 
    Enter values in structure
    5 hello
    8 world
    $ hexdump -vC new.c 
    00000000  05 00 00 00 68 65 6c 6c  6f 00 00 00 00 00 00 00  |....hello.......|
    00000010  08 00 00 00 77 6f 72 6c  64 00 00 00 00 00 00 00  |....world.......|
    00000020
    

    Notice that the first four bytes at offset 0x00 and 0x10 are the numbers entered (little-endian and 32-bit because of my machine), followed by the strings entered, plus a bit of structure padding. All broken down:

     File Offset     Data (ASCII)     Relationship to source
         0             05               var[0].a  7:0
         1             00               var[0].a 15:8
         2             00               var[0].a 23:16
         3             00               var[0].a 31:24
         4             68 (h)           var[0].ab[0]
         5             65 (e)           var[0].ab[1]             
         6             6c (l)           var[0].ab[2]
         7             6c (l)           var[0].ab[3]
         8             6f (o)           var[0].ab[4]
         9             00 (NUL)         var[0].ab[5]
        10             00 (NUL)         var[0].ab[6]
        11             00 (NUL)         var[0].ab[7]
        12             00 (NUL)         var[0].ab[8]
        13             00 (NUL)         var[0].ab[9]
        14             00               structure padding
        15             00               structure padding
        16             08               var[1].a  7:0
        17             00               var[1].a 15:8
        18             00               var[1].a 23:16
        19             00               var[1].a 31:24
        20             77 (w)           var[1].ab[0]
        21             6f (o)           var[1].ab[1]             
        22             72 (r)           var[1].ab[2]
        23             6c (l)           var[1].ab[3]
        24             64 (d)           var[1].ab[4]
        25             00 (NUL)         var[1].ab[5]
        26             00 (NUL)         var[1].ab[6]
        27             00 (NUL)         var[1].ab[7]
        28             00 (NUL)         var[1].ab[8]
        29             00 (NUL)         var[1].ab[9]
        30             00               structure padding
        31             00               structure padding