1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
| #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FILE_NAME "../data/file03.txt"
struct teacher
{
int age;
int id;
char *name;
int name_len;
};
int main(void)
{
FILE *fp = NULL;
#if 0
int write_ret = 0;
fp = fopen(FILE_NAME, "wb+");
if (fp == NULL) {
fprintf(stderr, "fopen error\n");
return -1;
}
struct teacher t1;
char *name = "zhang3";
t1.age = 10;
t1.id = 20;
t1.name = malloc(64);
memset(t1.name, 0, 64);
strcpy(t1.name, name);
t1.name_len = strlen(name);
write_ret = fwrite(&t1, sizeof(struct teacher), 1, fp);
if (write_ret < 0) {
fprintf(stderr, "write error\n");
return -1;
}
write_ret = fwrite(t1.name, t1.name_len, 1, fp);
if (write_ret < 0) {
fprintf(stderr, "write error\n");
return -1;
}
if (fp != NULL) {
fclose(fp);
}
#endif
// #if 0
struct teacher t2 = { 0 };
int read_ret = 0;
fp = fopen(FILE_NAME, "rb+");
if (fp == NULL) {
fprintf(stderr, "fopen r+error\n");
return -1;
}
read_ret = fread(&t2, sizeof(struct teacher), 1, fp);
if (read_ret < 0) {
fprintf(stderr, "fread error\n");
fclose(fp);
return -1;
}
t2.name = malloc(t2.name_len + 1);
memset(t2.name, 0, t2.name_len + 1);
read_ret = fread(t2.name, t2.name_len, 1, fp);
if (read_ret < 0) {
fprintf(stderr, "fread error\n");
fclose(fp);
return -1;
}
printf("id : %d, age : %d, name:%s, name_len :%d\n", t2.id, t2.age, t2.name, t2.name_len);
if (fp != NULL)
{
fclose(fp);
}
// #endif
return 0;
}
|