using fwrite() to write a struct to a file

后端 未结 3 1502
深忆病人
深忆病人 2021-01-18 08:54

I have the following program:

#include 
#include 
#include 

#define MAXLEN 100

typedef struct {int key; char         


        
3条回答
  •  悲哀的现实
    2021-01-18 09:48

    Several reasons:

    1. When you use scanf, it translates a human readable form (%d) into something the computer uses directly (int). You then write out the computer-readable form into a file. Now, when you view the file, you are NOT using the inverse computer-to-human-readable form but something much lower-level. This will give you something that looks wrong.
    2. You are writing out the entire x.data even though you may have read partial data into it (say, reading a string that's length 10). The remainder of the x.data is "uninitialized" and contains whatever was left in memory when main() was called.

提交回复
热议问题