-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonebook.c
More file actions
97 lines (86 loc) · 2.31 KB
/
phonebook.c
File metadata and controls
97 lines (86 loc) · 2.31 KB
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
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void compare(char *a, char *b);
int main(int argc, string argv[])
{
FILE *file;
long lsize;
char *buffer;
long result;
file = fopen("phonebook.txt", "a+");
if (file == NULL)
{
fputs("File error", stderr);
exit(1);
}
// fseek으로 file의 끝까지 읽어들인뒤 ftell을 통해 파일의 크기를 측정한다. 이후 rewind를 사용해 파일의 시작지점으로 돌아간다.
fseek(file, 0, SEEK_END);
lsize = ftell(file);
rewind(file);
// 전체 파일의 내용을 받을 수 있을 정도의 크기로 메모리를 할당한다.
buffer = malloc(lsizhele);
if (buffer == NULL)
{
fputs("Memory error", stderr);
exit(2);
}
// 그 파일의 내용을 버퍼에 저장한다.
result = fread(buffer, 1, lsize, file);
if (result != lsize)
{
fputs("Reading error", stderr);
exit(3);
}
//이름을 묻고 같은 값을 찾음
if(argc == 2 && strcmp(argv[1], "find") == 0)
{
char *name = get_string("Name : ");
compare(name, buffer);
return 0;
}
//이름과 번호 추가
if(argc == 2 && strcmp(argv[1], "add") == 0)
{
char *name = get_string("name : ");
char *number = get_string("number : ");
fprintf(file, "%s,%s\n", name, number);
printf("success\n");
}
//현재 find 커맨드를 사용했을때도 아래 문구가 출력되는 문제가 있어 우선 find 관련에 return을 사용하여 대충 해결해 두었다 원인은 뭘까?
else
{
printf("Worng command! (Please type add or find)\n");
}
fclose(file);
free(buffer);
return 0;
}
//같은 값을 찾는 과정
void compare(char *a, char *b)
{
char *search = strpbrk(b, a);
if (search)
{
int l = strlen(a);
if (strncmp(search, a, l) == 0)
{
printf("number : ");
for (int i = 1; i < 100; i++)
{
printf("%c", *(search + l + i));
if (*(search + l + i) == '\n')
{
return;
}
}
}
}
else
{
printf("not found\n");
return;
}
compare(a, b + 1);
}