이것 좀 도와주세요...ㅠㅠ
환찬
질문 제목 : 전화번호 관리 프로그램질문 요약:컴파일, 링크가 다 되었는데 실행이... ㅠㅠ
질문 내용 : 제가 열혈강의 도전문제4-7을작성했는데요, 삭제, 검색이 안 되요...ㅠㅠ 도와주세요.
소스코드
#includestdio.h
#includestring.h
typedef struct TelData{
char name[20];
char telphone[12];
}TelData;
TelData pArray[100];
TelData person;
void PrintMenu();
void Insert();
void Delete();
void Search();
void PrintAll();
enum{INSERT=1, DELETE, SEARCH, PRINT, EXIT};
int main()
{
int choice;
for( ; ; )
{
PrintMenu();
printf(Choose the item: );
scanf(%d, &choice);
switch(choice)
{
case INSERT:
Insert();
break;
case DELETE:
Delete();
break;
case SEARCH:
Search();
break;
case PRINT:
PrintAll();
break;
case EXIT:
return 0;
default:
printf(Illegal selection...\n);
break;
}
}
return 0;
}
void PrintMenu()
{
printf(-----Menu-----\n);
printf( 1.Insert\n);
printf( 2.Delete\n);
printf( 3.Search\n);
printf( 4.Print All\n);
printf( 5.Exit\n);
}
void Insert()
{
int state;
FILE* file;
printf([INSERT]\n);
printf(Input Name: );
scanf(%s, person.name);
printf(Input Tell Number: );
scanf(%s, person.telphone);
printf(------(--------Data Inserted\n);
/*file open*/
file=fopen(Data.txt, at);
if(file==NULL)
{
printf(data insert error!\n);
exit(1);
}
/*data insert*/
fprintf(file, %s %s\n, person.name, person.telphone);
/*file close*/
state=fclose(file);
if(state!=0)
{
printf(data insert error!\n);
exit(1);
}
}
void Delete()
{
int index1=0;
int i, j, state;
FILE* file;
char name2[20];
/*file open*/
file=fopen(Data.txt, rt);
if(file==NULL)
{
printf(data delete error!\n);
exit(1);
}
/*data read*/
for( ; ; )
{
fscanf(file, %s %s, pArray[index1].name, pArray[index1].telphone);
index1++;
if(feof(file)!=0)
break;
}
/*file close*/
state=fclose(file);
if(state!=0)
{
printf(data delete error!\n);
exit(1);
}
/*data delete*/
printf([DELETE]\n);
printf(Input Name : );
scanf(%s, name2);
for(i=0;iindex1;i++)
{
if(name2==pArray[i].name)
{
for(j=i;jindex1;j++)
{
strcpy(pArray[j].name, pArray[j+1].name);
strcpy(pArray[j].telphone, pArray[j+1].telphone);
}
index1--;
}
}
/*file open*/
file=fopen(Data.txt, wt);
if(file==NULL)
{
printf(data delete error!\n);
exit(1);
}
/*data rewrite*/
for(i=0;iindex1;i++)
fprintf(file,%s %s\n, pArray[i].name, pArray[i].telphone);
/*file close*/
state=fclose(file);
if(state!=0)
{
printf(data delete error!\n);
exit(1);
}
printf(--------Data Deleted\n);
}
void Search()
{
int index2=0;
int i;
int state;
FILE* file;
char name2[20];
/*file open*/
file=fopen(Data.txt, rt);
if(file==NULL)
{
printf(data search error!\n);
exit(1);
}
/*data read*/
for( ; ; )
{
fscanf(file, %s %s, pArray[index2].name, pArray[index2].telphone);
index2++;
if(feof(file)!=0)
break;
}
/*file close*/
state=fclose(file);
if(state!=0)
{
printf(data search error!\n);
exit(1);
}
/*data search*/
printf([SEARCH]\n);
printf(Input Name : );
scanf(%s, name2);
for(i=0;iindex2;i++)
{
if(name2==pArray[i].name)
{
printf(Name: %s /, pArray[i].name);
printf(Tel Phone: %s\n, pArray[i].telphone);
}
}
printf(--------Search Finish\n);
}
void PrintAll()
{
int index3=0;
int i;
int state;
FILE* file;
/*file open*/
file=fopen(Data.txt, rt);
if(file==NULL)
{
printf(data search error!\n);
exit(1);
}
/*data read*/
for( ; ; )
{
fscanf(file, %s %s, pArray[index3].name, pArray[index3].telphone);
index3++;
if(feof(file)!=0)
break;
}
/*file close*/
state=fclose(file);
if(state!=0)
{
printf(data search error!\n);
exit(1);
}
printf([PRINT ALL]\n);
for(i=0;iindex3;i++)
{
printf(Name : %s / , pArray[i].name);
printf(Tel Phone : %s\n, pArray[i].telphone);
}
printf(--------Printed All Data\n);
}
좀 긴가?? 첨부파일로도 올릴게요..
(확장자가 dsw, dsp모두 있어서 c원본 파일과 함께 같이 올립니다... VC++6.0임..)
p.s : 도전문제 4-7내용 - 전화번호 관리 프로그램을 만들어 보자. 기능은 입력, 삭제, 검색, (모두)출력이다. 데이터가 프로그램 종료 후에도 지워지지 않도록 파일을 하나 생성해서 데이터를 저장하는 방식을 취하자.
-
돋되다
정말 감사합니다!! 그런데 한 가지만 더 해결해 주세요...ㅠㅠ
실행이 잘 되는데 전화번호 여러 개를 저장한 후 제일 마지막의 것을 지운 다음
Print All선택하면 빈 공란(?)이 떠요... 자비좀..
어쨌든 감사^^ 항상 복이 가득할 거예요... -
HotPink
dsw, dsp 파일은 올리지 않아도 됩니다.
-
해까닥
if(name2==pArray[i].name) - if(!strcmp(name2,pArray[i].name))
문자열 비교를 하는데 괜히 문자열 비교함수가 있는게 아니겠죠.. 앞에 부분을 궂이 해석하자면
name2의 주소값과 pArray[i].name의 주소값이 같은지를 비교하게 되는거입니다. 이 부분은 중요하므로
공부를 필히하시구.. 그리고 exit(1) 함수에 대해 워닝이 뜨는데 #includestdlib.h 파일 안에 있으니
인클루트 해
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2695738 | fopen과fclose질문~~ (5) | 희선 | 2025-05-23 |
2695707 | 3의 배수 나타내기. (2) | 수리 | 2025-05-23 |
2695626 | 피보나치수열 과제 때문에 질문 드립니다. (6) | 옆집언니 | 2025-05-22 |
2695595 | 포인트공부중입니다 int형에서 4=1 인가요? (3) | 족장 | 2025-05-22 |
2695567 | 드라이브 고유번호를 가져오는 함수 (2) | 초코맛사탕 | 2025-05-21 |
2695533 | 음수의 산술변환! 질문이요 ㅠㅠ... (4) | 꽃여름 | 2025-05-21 |
2695506 | 구조체 배열 이용 도서목록 출력 프로그램 (1) | 가을귀 | 2025-05-21 |
2695450 | c언어 함수 질문이요.... | 이슬비 | 2025-05-20 |
2695403 | VirtualAlloc함수 및 메모리 질문 | 크리에이터 | 2025-05-20 |
2695355 | c언어 for함수 | 미쿡 | 2025-05-19 |
2695327 | 안녕하세요 제가 이번에 좀 큰 프로그램을.. | 악당 | 2025-05-19 |
2695295 | mutex동기화의 thread기반 채팅 서버소스 질문입니다 | 그루터기 | 2025-05-19 |
2695270 | 질문이요..swap 관한겁니다..ㅠㅠ (3) | 콩알녀 | 2025-05-19 |
2695244 | 노땅초보궁금한게 하나 있는데요..반복문(while문)초보자질문 (6) | 큰꽃늘 | 2025-05-18 |
2695166 | do while 문 어떤것이잘못된건지 모르겠어요 (2) | 아이폰 | 2025-05-18 |
2695122 | 구조체에 대해 물어보고 싶은게 있습니다 ^^^.. (7) | 수련 | 2025-05-17 |
2695091 | txt 파일 입출력 후 2차 배열에 저장하기입니다. (3) | 헛장사 | 2025-05-17 |
2695063 | 수도요금 프로그램좀 짜주세요. | 시내 | 2025-05-17 |
2695033 | 답변좀요ㅠㅠ (1) | 비사벌 | 2025-05-16 |
2695010 | C++의 STL은 왜 굳이 템플릿화 시켜서 라이브러리를 만드나요? (초보수준의 질문..) (2) | 엘보어 | 2025-05-16 |