#include<stdio.h>
#include<string.h>
#include <stdlib.h>
struct member{
char name[10];
char phone[13];
char null[1];
};
void show();
void search();
void add();
void del();
void change();
int main()
{
생략
}
void show()
{
FILE *f;
struct member m;
int index = 1;
if(f = fopen("phonebook.txt", "r")){
while(0 < fscanf(f, "%s %s",m.name, m.phone)){
printf("%d. ", index);
printf("%s %s\n",m.name, m.phone);
index ++;
}
fclose(f);
}
}
void search()
{
FILE *f;
struct member m;
int index = 1;
char name[40];
printf("검색할 이름을 입력 : ");
scanf("%s", name);
if(f = fopen("phonebook.txt", "r")){
while(0 < fscanf(f, "%s %s",m.name, m.phone)){
if(strcmp(name, m.name)==0)
printf("%d. %s %s\n\n",index, m.name, m.phone);
index ++;
}
fclose(f);
}
}
void add()
{
struct member m;
FILE* f;
f=fopen("phonebook.txt", "a");
printf("이름을 입력하세요: ");
scanf("%s", m.name);
printf("전화번호를 입력하세요: ");
scanf("%s", m.phone);
fprintf(f, "\n%s %s",m.name, m.phone);
fclose(f);
printf("\n저장 완료!\n\n");
}
void del()
{
FILE *f;
struct member m;
int index = 1;
char name[40];
printf("삭제할 이름을 입력 : ");
scanf("%s", name);
if(f = fopen("phonebook.txt", "w")){
while(0 < fscanf(f, "%s %s",m.name, m.phone)){
if(strcmp(name, m.name)==0){
strcpy(m.name, "");
strcpy(m.phone, "");
}
}
fclose(f);
}
}
void change()
{
}
처음으로 짜보는 프로그램인데 어렵네요.
이름 전화번호
가 행별로 적힌 phone.txt 파일을 읽어와서, 구조체 형태로 입/출력하는데
목록 출력하는거랑 검색은 어떻게어떻게 했거든요.
근데 삭제랑 바꾸는걸 모르겠어요.
삭제 저거 strcpy 함수 쓰니깐 목록 싹 날라가던데 ㅋ
원래 인터넷 뒤져보다가 구조체 배열로 저장해서 하는거 있길래 해보다가 자꾸 삐꾸나가지구
그냥 따로 버퍼 저장같은거 안하고 구조체만 가지고 하는 방법으로 했거든요. 삭제랑 변경하는건 그런 방법으로 안될까요 ㅠㅠ
댓글 0