How to write a struct to a file using fwrite?

后端 未结 6 1027
你的背包
你的背包 2021-01-21 09:20

I\'m very new to C, and I am having trouble with fwrite.

I\'m looking to use a struct that holds two values:

struct keyEncode{
    unsigned short key[2];         


        
6条回答
  •  灰色年华
    2021-01-21 09:45

    Look at your variable definitions, and look at which one you are using in the sizeof call. Is it correct to be specifying the size of a pointer rather than the size of the data? Also check your usage of fwrite, I think you are specifying the "count" parameter incorrectly. It is defined as

    size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
    

    The count parameter specifies the number of blocks of size size to write to the file.

    I don't really want to give you the answer right away (someone else probably will), as this is tagged as homework - you should be learning :)

提交回复
热议问题