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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct person {
char name[100];
char address[200];
char mobilePhone[40];
char desc[200];
struct person* link;
}Person;
void init_record(Person* p) {
sprintf(p->name, "null");
sprintf(p->address, "null");
sprintf(p->mobilePhone, "null");
sprintf(p->desc, "null");
p->link = NULL;
}
void loadFile(FILE* fp,Person* hp) {
Person* temp;
Person* last;
temp = (Person*)malloc(sizeof(Person));
fread(temp, sizeof(Person), 1, fp);
if (feof(fp) == 0){
printf("---------------------------------\n");
printf("Name : %s", temp->name);
printf("Address : %s", temp->address);
printf("Mobile Phone : %s", temp->mobilePhone);
printf("Description : %s", temp->desc);
hp->link = temp;
last = temp;
}
else free(temp);
temp = (Person*)malloc(sizeof(Person));
fread(temp, sizeof(Person), 1, fp);
if (feof(fp) == 0) {
printf("---------------------------------\n");
printf("Name : %s", temp->name);
printf("Address : %s", temp->address);
printf("Mobile Phone : %s", temp->mobilePhone);
printf("Description : %s", temp->desc);
last->link = temp;
last = temp;
}
else free(temp);
while (1){
temp = (Person*)malloc(sizeof(Person));
fread(temp, sizeof(Person), 1, fp);
if (feof(fp) == 0) {
printf("--------------------------------\n");
printf("Name : %s", temp->name);
printf("Address : %s", temp->address);
printf("Mobile Phone : %s", temp->mobilePhone);
printf("Description : %s", temp->desc);
last->link = temp;
last = temp;
}
else {
free(temp);
break;
}
}
printf("--------------------------------\n");
}
void add_record(FILE* fp, Person* hp) {
fp = fopen("info.dat", "a+b");
if (fp == NULL) {
printf("open error\n");
return;
}
Person* instPerson = (Person*)malloc(sizeof(Person));
init_record(instPerson);
Person* temp = hp;
while (temp->link != NULL) {
temp = temp->link;
}
getchar();
printf("input name:");
fgets(instPerson->name, sizeof(instPerson->name), stdin);
printf("input address:");
fgets(instPerson->address, sizeof(instPerson->address), stdin);
printf("input mobilePhone:");
fgets(instPerson->mobilePhone, sizeof(instPerson->mobilePhone), stdin);
printf("input desc:");
fgets(instPerson->desc, sizeof(instPerson->desc), stdin);
temp->link = instPerson;
fwrite(instPerson, sizeof(Person), 1, fp);
fclose(fp);
}
void search_record(Person* hp) {
char name[100];
printf("input name : ");
getchar();
fgets(name, sizeof(name), stdin);
//name[strlen(name) - 1] = '\0';
Person* temp = hp->link;
while (temp != NULL) {
if (strcmp(temp->name, name) == 0) {
printf("---------------------------------\n");
printf("Name : %s", temp->name);
printf("Address : %s", temp->address);
printf("Mobile Phone : %s", temp->mobilePhone);
printf("Description : %s", temp->desc);
printf("---------------------------------\n");
break;
}
temp = temp->link;
}
}
void update_record(Person* hp) {
char name[100];
printf("input name : ");
getchar();
fgets(name, sizeof(name), stdin);
Person* temp = hp->link;
FILE* fpw = fopen("info_temp.dat", "wb");
if (fpw == NULL) {
printf("open error\n");
return;
}
while (temp != NULL) {
if (strcmp(temp->name, name) == 0) {
printf("input address:");
fgets(temp->address, sizeof(temp->address), stdin);
printf("input mobilePhone:");
fgets(temp->mobilePhone, sizeof(temp->mobilePhone), stdin);
printf("input desc:");
fgets(temp->desc, sizeof(temp->desc), stdin);
}
fwrite(temp, sizeof(Person), 1, fpw);
temp = temp->link;
}
fclose(fpw);
remove("info.dat");
rename("info_temp.dat", "info.dat");
}
void printAll(Person* hp) {
Person* temp = hp->link;
while (temp != NULL) {
printf("---------------------------------\n");
printf("Name : %s", temp->name);
printf("Address : %s", temp->address);
printf("Mobile Phone : %s", temp->mobilePhone);
printf("Description : %s", temp->desc);
temp = temp->link;
}
printf("---------------------------------\n");
}
void main() {
int num = 0;
Person* hp = (Person*)malloc(sizeof(Person));
init_record(hp);
FILE* fp;
fp = fopen("info.dat", "rb");
if (fp != NULL) {
loadFile(fp, hp);
fclose(fp);
}
while (true) {
printf("------------------------------\n");
printf("1. Add\n");
printf("2. Modify\n");
printf("3. Search\n");
printf("4. Delete\n");
printf("5. Print All The Members\n");
printf("6. Quit\n");
printf("------------------------------\n");
scanf("%d", &num);
if (num == 6)
break;
switch (num) {
case 1:
add_record(fp, hp);
break;
case 2:
update_record(hp);
break;
case 3:
search_record(hp);
break;
case 4:
break;
case 5:
printAll(hp);
break;
default:
break;
}
}
{
Person* temp = hp;
Person* temp2;
while(temp!=NULL)
{
temp2 = temp->link;
free(temp);
temp = temp2;
}
}
} |
cs |
C언어 파일입출력 및 연결리스트를 활용한 사용자 관리 프로그램 입니다.
'컴퓨터 공학 > C++' 카테고리의 다른 글
[C/C++] C++과 SQLite 연동하기 (0) | 2016.02.29 |
---|---|
C언어로 파워볼 당첨 시뮬레이터 만들기 (0) | 2016.01.14 |
[공유] 씹어먹는 C 언어 - <24. 더 빠르게 실행되는 코드를 위하여 (C 코드 최적화)> (0) | 2015.11.27 |
[퍼옴] 정말 C는 C++보다 빠를까? (0) | 2015.11.27 |
C++ 파일 입출력 (0) | 2015.11.27 |