#include <stdio.h>
#include <string.h> // strcmp

#define CAPACITY 100
#define BUFFER_SIZE 100

char * names[CAPACITY];
char * numbers[CAPACITY];
int n = 0;

void add(); //전화번호 추가 함수
void find(); //사람 찾기 함수
void status(); //전체 저장 된 사람 찾기 함수
void delete();
void load(); //사람 삭제 함수 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2
void save(); //저장 함수
int search(); //사람 검색 부가함수


int main()
{
char command[BUFFER_SIZE]; // 명령어를 받기 위한 일종의 배열 (버퍼)
while (1)
{
printf("$ ");
scanf("%s", command);

if (strcmp(command, "read") == 0)
load();
else if (strcmp(command, "add") == 0) // 입력한 문자열과 뒷부분 " " 를 비교
add(); // 비교하여 같으면 0을 반환하기에 그 함수가 실행됨
else if (strcmp(command, "find") == 0)
find();
else if (strcmp(command, "status") == 0)
status();
else if (strcmp(command, "delete") == 0)
delete(); //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2
else if (strcmp(command, "save") == 0)
save();
else if (strcmp(command, "exit") == 0)
break;
}
return 0;
}

void load()
{
char fileName[BUFFER_SIZE];
char buf1[BUFFER_SIZE];
char buf2[BUFFER_SIZE];

scanf("%s", fileName);

FILE *fp = fopen(fileName, "r");
if (fp==NULL)
{
printf("Open Failed.\n");
return;
}
while ((fscanf(fp, "%s", buf1) != EOF))
{
fscanf(fp, "%s", buf2);
names[n] = strdup(buf1);
numbers[n] = strdup(buf2);
n++;
}
fclose(fp);
}
void add()
{
char buf1[BUFFER_SIZE], buf2[BUFFER_SIZE];
scanf("%s", buf1);
scanf("%s", buf2);

int i = n-1;
while (i>=0 && strcmp(names[i], buf1) > 0)
{
names[i+1] = names[i];
numbers[i+1] = numbers[i];
i--;
}
//strdup란 문자열을 복제 = duplication
names[n] = strdup(buf1); //이름 저장
numbers[n] = strdup(buf2);//번호 저장
n++;
printf("%s was added successfully.\n", buf1);
}

void find() //사람 찾기 함수
{
char buf[BUFFER_SIZE];
scanf("%s", buf);
int index = search(buf); //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@22
if (index == -1)
{
printf("%s is not exist.\n", buf);
}
else
{
printf("%s\n", numbers[index]);
}
}

int search(char *name) //사람 검색 부가함수 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
{
int i;
for (i = 0; i < n; i++)
{
if (strcmp(name, names[i]) == 0)
{
return i;
}
}
return -1;
}


void status() //전체 저장 된 사람 찾기 함수
{
int i;
for (i = 0; i < n; i++)
{
printf("%s %s\n", names[i], numbers[i]);
}
printf("Total %d person.\n", n);
}


void delete() //사람 삭제 함수 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
{
char buf[BUFFER_SIZE];
scanf("%s", buf);

int index = search(buf); //To do : search(); //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2
if (index == -1)
{
printf("%s is not exist.\n", buf);
return;
}
int j = index;
for (; j < n-1; j++)
{
names[j] = names[j+1];
numbers[j] = numbers[j+1];
}
n--;
printf("%s was deleted successfully.\n", buf);
}


void save()
{
int i;
char fileName[BUFFER_SIZE];
char tmp[BUFFER_SIZE];

scanf("%s", tmp);
scanf("%s", fileName);

FILE *fp = fopen(fileName,"w"); // 파일을 쓸 때는 모드를 "w"로 하고 열어야 한다

if (fp==NULL)
{
printf("Open failed\n");
return;
}
for (i = 0; i < n; i++)
{
fprintf(fp, "%s %s\n", names[i], numbers[i]);
}
fclose(fp);
}



빌드랑 실행은 됩니다 ㅠ