Search code examples
cstructurefwrite

Where and how to use fwrite function in c


I just can't understasnd how fwrite finction works in c.I had made a c program.here it it:-

#include <stdio.h>

int main()
{
struct books
{
    char name[100];
    float price;
    int pages;
}book1;

puts("enter the name of the book");gets(book1.name);
puts("enter the price of the book");scanf("%f",&book1.price);
puts("enter the pages of the book");scanf("%d",&book1.pages);

FILE *b=fopen("Book.txt","w");
fwrite(&book1,sizeof(book1),1,b);
}

here is input of th program:-

 $ ./a.out 
 enter the name of the book
 Voldemort the last BSD user
 enter the price of the book
 40000
 enter the pages of the book
 34

and here is the content of the file:-

 $ cat Book.txt 
  Voldemort the Great Linux and bsd user!����c����@�@��,B

Thus what is wrong in my program ? How does fwrite works ? When should I use it?


Solution

  • Nothing is immediately wrong with your program, except that you use gets(), which is very wrong (a guaranteed buffer overflow), but has nothing to do with your question, so I just leave the hint here: Use fgets() and for general information how to reliably get input in C, I recommend reading

    As for your question, fwrite() writes whatever you give it to a file in exactly the same way it's in the memory of your computer. You could read this same file again with fread() and end up with the same struct. But be aware the representations of data types are implementation defined. So you can't for example read this file on a different machine, or even on the same machine with a program compiled using a different compiler. Therefore, using fwrite() to write whole structs is of very limited use in practice.

    As for why you see "strange stuff" with cat, that's simply because cat interprets all file contents as characters. You would see the same if you would just write your float and int to stdout directly, instead of formatting it with printf().

    To write structs in a portable way to a file, you have to apply some sort of data serialization. A very simple way would be to use fprintf() with a sensible format string. There are many possibilities to do that, you could e.g. use a JSON format with an appropriate JSON library, same goes for XML.