สมุดโทรศัพท์ ตอนนี้ในส่วนของโปรแกรมสมุดโทรศัพท์ตอนนี้เรียบร้อนหมดแล้ว ยังเหลือส่วนที่เป็นฟังก์ชั่นลบ ขอช่วยหน่อยคะ
#include<stdio.h> #include<conio.h> #include<string.h>
int data(){ FILE *fp; char Tel[10],name[20],Nname[10],email[20]; fp=fopen("file.txt","r"); if(fp==NULL){ printf("Cannot open file.txt\n"); return 1; } printf("Phonebook\n\n\n"); while(!feof(fp)){ fscanf(fp,"%s%s%s%s",&Tel,&name,&Nname,&email); printf("%s\t%s\t%s\t%s\n",Tel,name,Nname,email); } fclose(fp); getch(); }
int Search(){ bool found=false; FILE *fp; char Tel[10],name[20],Nname[10],email[20],tempTel[20]; fp=fopen("file.txt","r"); if(fp==NULL){ printf("Cannot open file.txt\n"); return 1; } printf("Phonebook\n\n\n"); printf("input text to sear: "); scanf("%s",tempTel); while(!feof(fp)){ fscanf(fp,"%s%s%s%s",&Tel,&name,&Nname,&email); if(strcmp(Tel,tempTel)==0){ printf("%s\t%s\t%s\t%s\n",Tel,name,Nname,email); found=true; } if(strcmp(name,tempTel)==0){ printf("%s\t%s\t%s\t%s\n",Tel,name,Nname,email); found=true; } if(strcmp(Nname,tempTel)==0){ printf("%s\t%s\t%s\t%s\n",Tel,name,Nname,email); found=true; } if(strcmp(email,tempTel)==0){ printf("%s\t%s\t%s\t%s\n",Tel,name,Nname,email); found=true; } } fclose(fp); if(!found) printf("Not found\n"); }
void insert(){
char Tel[15],name[20],Nname[10],email[200]; FILE *fp; fp = fopen("file.txt","a"); while (1){ printf("#### Input Data ####\n"); printf("enter your phonenumber:"); scanf("%s",Tel); if (strlen(Tel)<= 9) break; printf("enter your name:"); scanf("%s",name);
printf("enter your nickname:"); scanf("%s",Nname); printf("enter your email:"); scanf("%s",email); fprintf(fp,"%s %s %s %s\n",Tel,name,Nname,email); } fclose(fp); } int delete_data(){ FILE *fp; int i,n,m,k; char Tel[20][20],name[20][30],Nname[20][20],email[20][40]; fp=fopen("file.txt","r"); if(fp==NULL){ printf("Cannot open file.txt\n"); return 1; } printf("Phonebook\n\n\n"); n=0; while(!feof(fp)){ n++; fscanf(fp,"%s%s%s%s",&Tel[n],&name[n],&Nname[n],&email[n]); // printf("%2d. %s\t%s\t%s\t%s\n",n,Tel[n],name[n],Nname[n],email[n]); } fclose(fp); for(i=1;i<n ;i++){ printf("%2d. %s\t%s\t%s\t%s\n",i,Tel[i],name[i],Nname[i],email[i]); } printf(" *****Enter you To Delet***** \n"); scanf("%d",&m); for(i=1;i<n ;i++){ printf("%2d. %s\t%s\t%s\t%s\n",i,Tel[i],name[i],Nname[i],email[i]); } fp=fopen("file.txt","w"); if(fp==NULL){ printf("Cannot open file.txt\n"); return 1; } for(i=1;i<n ;i++){ fprintf(fp,"%s\t%s\t%s\t%s\n",Tel[i],name[i],Nname[i],email[i]); } fclose(fp); getch(); }
main() { int n; do{ printf("---------- Menu Phonebook-----------\n"); printf("------1 --> Input Data\n"); printf("------2 --> Show\n"); printf("------3 --> Search\n"); printf("------4 --> Delete\n"); printf("------0 --> Exit\n"); printf(" Please select Menu :: "); scanf("%d",&n); switch (n) { case 1: insert(); break; case 2: data(); break; case 3: Search(); break; case 4: delete_data(); break; case 0: return 0; } }while(n > 0 && n <= 2); }
|