#include <stdio.h>
#include <string.h>
#define bufsize 500
int line(char *path)
{
int count=0;
char line[100]={0};
//char pathopen[100];
FILE *fp;
//strcpy(pathopen, path);
fp = fopen(path,"r");
if( NULL == fp )
{
printf("Pleas load record fisrt!\n");return 1;
}
while(fgets(line,sizeof(line)-1,fp)){
count++;
}
return count;
fclose(fp);
}
void showrecord(char *path,int loccount)
{
int i,linenumber;
char bufname[bufsize],bufadd[bufsize],bufcelnam[bufsize];
FILE *fp;
struct gueststu{
char guest_name[20];
char guest_address[500];
char guest_phone[20];
};
struct gueststu guest[20];
fp = fopen(path,"r");
linenumber = line(path);
for( i = 0 ;i < (linenumber/3) ; i++ ){
fgets(bufname ,bufsize ,fp);
fgets(bufadd ,bufsize ,fp);
fgets(bufcelnam ,bufsize ,fp);
strcpy(guest[i].guest_name,bufname);
strcpy(guest[i].guest_address,bufadd);
strcpy(guest[i].guest_phone,bufcelnam);
}
fclose(fp);
if ( loccount == -1){
for( i =0 ;i < (linenumber/3) ; i++ ){
printf("Address is %sname is %sPhone number is %s \n",guest[i].guest_address,guest[i].guest_name,guest[i].guest_phone);
}
}
else
printf("Address is %sName is %sPhone number is %s \n",guest[loccount].guest_address,guest[loccount].guest_name,guest[loccount].guest_phone);
}
int searchrecord(char *path)
{
char scanfbuf[bufsize],buf[bufsize];
int count = 0,i;
FILE *fp;
fp = fopen(path, "r");
printf("Enter a name");
scanf("%s",scanfbuf);getchar();
printf("Search %s \n\n",scanfbuf);
for(i = 0;i < line(path) ; i++){
fgets(buf, bufsize, fp);
buf[strlen(buf)-1] = '\0';
//printf("%s",buf);scanf("%d",&i);
if (strcmp(buf,scanfbuf) == 0)
{
//printf("It's the same one");
break;
}
else
count++;
}
if(strcmp(buf,scanfbuf) != 0){
printf("No this guest!\n");
return 1;
}
//printf(" in line %d \n",count);
count = count/3;
showrecord(path,count);
return 0;
}
int deleterecord(char *path)
{
char scanfbuf[100];
int j,i,k,linenumber,flag;
char bufname[bufsize],bufadd[bufsize],bufcelnam[bufsize];
FILE *fp;
struct gueststu{
char guest_name[20];
char guest_address[500];
char guest_phone[20];
};
struct gueststu guest[20];
linenumber = line(path);
fp = fopen(path, "r");
for( i = 0 ;i < (linenumber/3) ; i++ )
{
fgets(bufname ,bufsize ,fp);
fgets(bufadd ,bufsize ,fp);
fgets(bufcelnam ,bufsize ,fp);
strcpy(guest[i].guest_name,bufname);
guest[i].guest_name[strlen(guest[i].guest_name)-1] = '\0';
strcpy(guest[i].guest_address,bufadd);
guest[i].guest_address[strlen(guest[i].guest_address)-1] = '\0';
strcpy(guest[i].guest_phone,bufcelnam);
guest[i].guest_phone[strlen(guest[i].guest_phone)-1] = '\0';
}
printf("Enter a name : ");
scanf("%s",scanfbuf);getchar();
//printf("%s***\n\n",scanfbuf);
for(flag=1,j=0;j<9;j++)
{
//printf("%s",guest[j].guest_name);scanf("%d",&k);
k = strcmp (scanfbuf,guest[j].guest_name);
//printf("%d",k);scanf("%d",&i);
if ( k == 0 )
{
for(i = j;i < (linenumber/3); i++ )
{
strcpy(guest[i].guest_address,guest[i+1].guest_address);
strcpy(guest[i].guest_name,guest[i+1].guest_name);
strcpy(guest[i].guest_phone,guest[i+1].guest_phone);
}flag = 0;
}
}
if(!flag)
linenumber=linenumber-3;
else
{
printf("No Found!\n\n");
return 1;
}
fclose(fp);
fp = fopen(path, "w");
for( i = 0 ;i < (linenumber/3) ; i++ ){
//printf("address is %s\nname is %s\nphone number is %s \n",guest[i].guest_name,guest[i].guest_address,guest[i].guest_phone);
fprintf(fp,"%s\n%s\n%s\n",guest[i].guest_name,guest[i].guest_address,guest[i].guest_phone);
}
printf("The record has been deleted!\n");
fclose(fp);
return 0;
}
int main(int argc, const char * argv[])
{
int cmd;
char path[100],ext;
path[0] = 0;
menu:do{
printf("******************************\n");
printf("1.Load record\n");
printf("2.Show record\n");
printf("3.Delete record\n");
printf("4.Inquiry record\n");
printf("5.Quit\n");
printf("******************************\n");
printf("Please choice : ");
scanf("%d",&cmd);
}while (cmd > 5||cmd<0);
switch ( cmd ){
case 1:{
FILE *fp;
for( ; ; ){
printf("Please input the file and path name : ");
scanf("%s",path);getchar();
fp = fopen(path,"r");
if ( NULL == fp){
printf("The path is invaild\n");
continue;
}
break;
}
fclose(fp);
}goto menu;
case 2:{
if (path[0] == 0){
printf("Please load record first!\n");
goto menu;
}
showrecord(path,-1);}goto menu;
case 3:{
if (path[0] == 0){
printf("Please load record first!\n");
goto menu;}
deleterecord(path);
}goto menu;
case 4:{
if (path[0] == 0){
printf("Please load record first!\n");
goto menu;
}
searchrecord(path);}goto menu;
case 5:{
printf("Do you want to quit this programme?\n");
scanf("%s",&ext);getchar();
if ((ext == 'y')|| (ext == 'Y')){
printf("Thank you for using this programme!");
return 10;
}
else
goto menu;
}
}
return 0;
}
如何增加一个通讯录信息,信息包括姓名,地址和电话
最新推荐文章于 2021-11-25 16:13:55 发布