2 * A fread() and fwrite() example
17 static void usage(const char *progname
)
19 printf("Usage: %s < -w | -r > < filename >\n", progname
);
22 int main(int argc
, char *argv
[])
28 char *filepath
, *progname
;
31 fprintf(stderr
, "Wrong number of arguments\n");
38 if (!memcmp(argv
[1], "-w", 2)) {
42 * Write the structures using fwrite()
45 file
= fopen(filepath
, "w+");
52 memset(datav
, 0, sizeof(data
));
55 strncpy(datav
[0].name
, "luiz", NAME_MAX
);
59 strncpy(datav
[1].name
, "capitulino", NAME_MAX
);
61 ret
= fwrite((void *) datav
, sizeof(struct data
),
72 * Write the structures using fwrite()
75 file
= fopen(filepath
, "r");
81 for (i
= 0; i
< 2; i
++) {
82 ret
= fread((void *) &data
, sizeof(data
), 1, file
);
85 fprintf(stderr
, "finished reading\n");
93 printf("id = %d\ncount = %d\nname = %s\n\n", data
.id
,
94 data
.count
, data
.name
);