| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1460 人关注过本帖
标题:C语言通讯录,其中有查询函数不有问题,但出在哪里?WIN-TC
只看楼主 加入收藏
a65931536
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-6-20
收藏
 问题点数:0 回复次数:7 
C语言通讯录,其中有查询函数不有问题,但出在哪里?WIN-TC
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct xx
{
   char xingming[15];
   char xingbie[5];
   char dianhua[13];
   char qq[13];
   char email[30];
};
int printf1()                              /*菜单函数*/
{
   int i;
   clrscr();
   printf("\t\t\t\tTong Xun Lu\n");
   printf("--------------------------------------------------------------------------------");
   printf("1.Liu Lan    ");
   printf("2.Tian Jia    ");
   printf("3.Xiu Gai    ");
   printf("4.Cha Zhao    ");
   printf("5.Shan Chu    ");
   printf("6.Tui Chu");
   printf("\n--------------------------------------------------------------------------------");
   printf("Wo Yao Xuan Ze:");
   scanf("%d",&i);
   return i;
}
tj()                                       /*添加函数*/
{
   int a;
   struct xx XX;
   FILE *fp;
   if((fp=fopen("xinxi.txt","ab"))==NULL)
   {
     printf("Error....Can't nofind the file!\n");
     getch();
     return main();
     }
   printf("Shu Ru Xing Ming:");
   gets(XX.xingming);
   clrscr();

   printf("Shu Ru Xing Ming:");
   gets(XX.xingming);

   printf("Shu Ru Xing Bie:");
   gets(XX.xingbie);

   printf("Shu Ru Dian Hua:");
   gets(XX.dianhua);

   printf("Shu Ru QQ:");
   gets(XX.qq);

   printf("Shu Ru E-mail:");
   gets(XX.email);

   fwrite(&XX,sizeof(XX),1,fp);
   fclose(fp);

   printf("\n1.Fanhui--2.Tianjia--3.Tuichu-->[ ]\b\b");
   scanf("%d",&a);

   if(a==1)
   {
     return main();
     }
     else if(a==2)
     {
   return tj();
   }
   else if(a==3)
   {
      exit(0);
      }
   else
   {
    printf("Error!Please cry again!\n");
    getch();
    return main();
    }
   }
xs()                                 /*显示函数*/
{
    FILE *fp;
    int i;
    struct xx XX;
    if((fp=fopen("xinxi.txt","rb"))==NULL)
      {
        printf("Error....Can't nofind the file!");
        getch();
        main();
        }
     while(fread(&XX,sizeof(XX),1,fp)==1)
       {
          printf("Xingming: %s\n",XX.xingming);
          printf("Xingbie : %s\n",XX.xingbie);
          printf("Dianhua : %s\n",XX.dianhua);
          printf("QQ      : %s\n",XX.qq);
          printf("E-mail  : %s\n\n",XX.email);
          }
     fclose(fp);
     printf("(1.fanhui||2.tuichu)-->[ ]\b\b");
     scanf("%d",&i);
     if(i==1)
     {
        return main();
        }
        else if(i==2)
        {
          exit(1);
          }
          else
          printf("Error....Please input 1 or 2!");
          getch();
          clrscr();
          xs();
     }
cx()                               /*查询函数*/
{
    struct xx XX;
    FILE *fp;
    int flag;                      /*判断查询条件*/
    int total;
    char ch;
    int b;
    int tel;
    if((fp=fopen("xinxi.txt","rb"))==NULL)
      {
         printf("Error....Can't nofind the file!");
         getch();
         main();
         }
      rewind(fp);
      printf("(1.name||2.tel)-->[ ]\b\b");
      scanf("%d",&flag);
      if(flag==1)                  /*按姓名查询*/
      {
        printf("Please input the name you want to search:");
        scanf("%s",&ch);
        total=0;
        while(fread(&XX,sizeof(XX),1,fp)==1)
           if(strcmp(ch,XX.xingming)==1)
             {
               total++;
               if(total==1)
               printf("\nXing Ming:%s\nXing Bie:%s",XX.xingming,XX.xingbie);
               }
               }
       if(flag==2)                  /*按电话查询*/
         {
           printf("Please input tel you want to search:");
           scanf("%s",&tel);
           total=0;
           while(fread(&XX,sizeof(XX),1,fp)==1)
             if(strcmp(tel,XX.dianhua)==0)
             {
               total++;
               if(total==1)
               printf("\nXing Ming:%s\nXing Bie:%s",XX.xingming,XX.xingbie);
               }
               }
               fclose(fp);
               printf("1.fanhui||2.tuichu-->[ ]\b\b");
               scanf("%d",&b);
               if(b==1)
               {
                 return main();
                 }
                 else
                 {
                 exit(0);
                 }
                 }
xg()                                 /*修改函数*/
{
   struct xx XX;
   FILE *fp1,*fp2;
   int flag;
   char name;
   int a;
   if((fp1=fopen("xinxi.txt","rb"))==NULL)
   {
        printf("Error....Can't nofind the file!");
        getch();
        main();
        }
    if((fp2=fopen("temp.txt","wb"))==NULL)
    {
       printf("Error....Can't nofind the file!");
        getch();
        main;
        }
        printf("Please input the name you want to modify:");
        scanf("%s",&name);
        flag=0;
        while(fread(&XX,sizeof(XX),1,fp1)==1)
        {
          if(strcmp(name,XX.xingming)==0)
          {
            printf("\nXingming:%s\n",XX.xingming);
            printf("Xingming  :%s\n",XX.xingbie);
            printf("Xingming  :%s\n",XX.dianhua);
            printf("Xingming  :%s\n",XX.qq);
            printf("Xingming  :%s",XX.email);
            printf("\n\n\nShu Ru Xin Ji Lu\n");
            printf("Xingming:");
            gets(XX.xingming);
            printf("Xingbie:");
            gets(XX.xingbie);
            printf("Dianhua:");
            gets(XX.dianhua);
            printf("QQ:");
            gets(XX.qq);
            printf("E-mail:");
            gets(XX.email);
            flag=1;
            }
            fwrite(&XX,sizeof(XX),1,fp2);
            }
            fclose(fp1);
            fclose(fp2);
            if(flag==1)
            {
              printf("Modify sucess!\n");
              remove("xinxi.txt");
              rename("temp.txt","xinxi.txt");
              }
              else
              printf("Can not find this recore!\n");
              getch();
              printf("1.Jixu||2.Fanhui||3.Tuichu-->[ ]\b\b");
              scanf("%d",&a);
              if(a==1)
              {
              clrscr();
              xg();
              }
              else if(a==2)
              {
                 main();
                 }
                 else if(a==3)
                 {
                   exit(1);
                   }
                   else
                   printf("Error!Please input 1,2,3!OK?");
                   clrscr();
                   xg();
              }



main()
{
   switch(printf1())
   {
     case 1:clrscr();xs();break;
     case 2:clrscr();tj();break;
     case 3:clrscr();xg();break;
     case 4:clrscr();cx();break;
     case 5:clrscr();printf("Deng Dai");getch();break;
     case 6:exit(0);break;
     default:clrscr();printf("Error.....Please perss any key!");getch();main();break;
     }
          }
搜索更多相关主题的帖子: C语言 函数 通讯录 printf include 
2008-07-16 01:40
a65931536
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-6-20
收藏
得分:0 
大家不嫌麻烦的话,把这段代码复制下来,在自己的编译器里运行一下,其实有很多问题,不太大,但我很不爽
希望大家能帮我揭开这个疙瘩
2008-07-16 01:43
chengli
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-7-3
收藏
得分:0 
现在只有头两个函数,下次再改
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct xx
{
   char xingming[15];
   char xingbie[5];
   char dianhua[13];
   char qq[13];
   char email[30];
};
int printf1()                              /*菜单函数*/
{
   int i;
   clrscr();
   printf("\t\t\t\tTong Xun Lu\n");
   printf("--------------------------------------------------------------------------------");
   printf("1.Liu Lan    ");
   printf("2.Tian Jia    ");
   printf("3.Xiu Gai    ");
   printf("4.Cha Zhao    ");
   printf("5.Shan Chu    ");
   printf("6.Tui Chu");
   printf("\n--------------------------------------------------------------------------------");
   printf("Wo Yao Xuan Ze:");
   scanf("%d",&i);
   return i;
}
tj()                                       /*添加函数*/
{
   struct xx XX;
   FILE *fp;
   if((fp=fopen("xinxi.txt","a"))==NULL)
   {
     printf("Error....Can't nofind the file!\n");
     getch();
     return;
     }
   clrscr();
   getchar();   /*用来接收'\n'*/
   printf("Shu Ru Xing Ming:");
   gets(XX.xingming);

   printf("Shu Ru Xing Bie:");
   gets(XX.xingbie);

   printf("Shu Ru Dian Hua:");
   gets(XX.dianhua);

   printf("Shu Ru QQ:");
   gets(XX.qq);

   printf("Shu Ru E-mail:");
   gets(XX.email);
    fprintf(fp,"%15s,%5s,%13s,%13s,%30s",XX.xingming,XX.xingbie,XX.dianhua,XX.qq,XX.email);
    fputc('\n',fp);
   fclose(fp);
    }


xs()                                 /*显示函数*/
{
    FILE *fp;
    struct xx XX;
    if((fp=fopen("xinxi.txt","r"))==NULL)
      {
        printf("Error....Can't nofind the file!");
        return;
        }
     while(!feof(fp))
       {
      fputc(fgetc(fp),stdout);
          }
    getch();
    getchar(); /*用来停顿*/
     fclose(fp);
     }
cx()                               /*查询函数*/
{
    struct xx XX;
    FILE *fp;
    int flag;                      /*判断查询条件*/
    char ch[15];
    char tel[13];
    if((fp=fopen("xinxi.txt","r"))==NULL)
      {
         printf("Error....Can't nofind the file!");
         return;
         }
      printf("(1.name||2.tel)-->[ ]\b\b");
  do
{
  scanf("%d",&flag);
      if(flag==1)                  /*按姓名查询*/
      {
        printf("Please input the name you want to search:");
        scanf("%s",&ch);
        while(fread(&XX,sizeof(XX),1,fp)==1)
           if(strcmp(ch,XX.xingming)==0)
             {
        printf("%15s,%5s,%13s,%13s,%30s",XX.xingming,XX.xingbie,XX.dianhua,XX.qq,XX.email);
        break;
           }
    }
       if(flag==2)                  /*按电话查询*/
         {
           printf("Please input tel you want to search:");
           scanf("%s",&tel);
           while(fread(&XX,sizeof(XX),1,fp)==1)
             if(strcmp(tel,XX.dianhua)==0)
        {
        printf("%15s,%5s,%13s,%13s,%30s",XX.xingming,XX.xingbie,XX.dianhua,XX.qq,XX.email);
        break;
               }
         }
}while(flag<1||flag>2);
               fclose(fp);
 }
xg()                                 /*修改函数*/
{
   struct xx XX;
   FILE *fp1,*fp2;
   char name[15];
   if((fp1=fopen("xinxi.txt","r"))==NULL)
   {
        printf("Error....Can't nofind the file!");
       return;
        }
    if((fp2=fopen("temp.txt","w"))==NULL)
    {
       printf("Error....Can't nofind the file!");
    fclose(fp1);
        return;
     }
        printf("Please input the name you want to modify:");
        scanf("%s",&name);
       while(fread(&XX,sizeof(XX),1,fp1)==1)
        {
          if(strcmp(name,XX.xingming)==0)
          {
            printf("\nXingming:%s\n",XX.xingming);
            printf("Xingming  :%s\n",XX.xingbie);
            printf("Xingming  :%s\n",XX.dianhua);
            printf("Xingming  :%s\n",XX.qq);
            printf("Xingming  :%s",XX.email);
            printf("\n\n\nShu Ru Xin Ji Lu\n");
            printf("Xingming:");
            gets(XX.xingming);
            printf("Xingbie:");
            gets(XX.xingbie);
            printf("Dianhua:");
            gets(XX.dianhua);
            printf("QQ:");
            gets(XX.qq);
            printf("E-mail:");
            gets(XX.email);
            }
fprintf(fp2,"%15s,%5s,%13s,%13s,%30s",XX.xingming,XX.xingbie,XX.dianhua,XX.qq,XX.email);
fputc('\n',fp2);
            }
            fclose(fp1);
            fclose(fp2);
        remove("xinxi.txt");
               rename("temp.txt","xinxi.txt");
}

main()
{
    do
  {
    switch(printf1())
   {
     case 1:xs();break;
     case 2:clrscr();tj();break;
     case 3:clrscr();xg();break;
     case 4:clrscr();cx();break;
     case 5:clrscr();printf("Deng Dai");break;
     case 6:exit(0);break;
     default:clrscr();printf("Error.....Please perss any key!");break;
     }
  } while(1);
}
2008-07-16 21:42
chengli
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-7-3
收藏
得分:0 
我也是菜鸟
应该是fread()用错了吧
我今天也不想改了
明天再改吧
你错的比较多
2008-07-16 21:59
chengli
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-7-3
收藏
得分:0 
现在已经都改好了,代码比上次的要好多。。。。。你好好借鉴吧
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct xx
{
   char xingming[15];
   char xingbie[5];
   char dianhua[13];
   char qq[13];
   char email[30];
};
int printf1()                              /*菜单函数*/
{
   int i;
   clrscr();
   printf("\t\t\t\tTong Xun Lu\n");
   printf("--------------------------------------------------------------------------------");
   printf("1.Liu Lan    ");
   printf("2.Tian Jia    ");
   printf("3.Xiu Gai    ");
   printf("4.Cha Zhao    ");
   printf("5.xin  jian   ");
   printf("6.Tui Chu");
   printf("\n--------------------------------------------------------------------------------");
   printf("ni Yao Xuan Ze:");
   scanf("%d",&i);
   return i;
}
create()
{
    FILE *fp;
    if(!(fp=fopen("xinxi.txt","w")))
    {
        printf("%s","cannot create  ");
        return;
    }
    fprintf(fp,"%13s  %5s%15s%15s%28s\n","xingming","xingbie","dianhua","qq","email");
        fclose(fp);
}

tj()                                       /*添加函数*/
{
   struct xx XX;
   FILE *fp;
   if((fp=fopen("xinxi.txt","a"))==NULL)
   {
     printf("Error....Can't nofind the file!\n");
     getch();
     return;
     }
   clrscr();
   getchar();   /*用来接收'\n'*/
   printf("Shu Ru Xing Ming:");
   gets(XX.xingming);

   printf("Shu Ru Xing Bie:");
   gets(XX.xingbie);

   printf("Shu Ru Dian Hua:");
   gets(XX.dianhua);

   printf("Shu Ru QQ:");
   gets(XX.qq);

   printf("Shu Ru E-mail:");
   gets(XX.email);
    fprintf(fp,"%15s%5s%15s%15s%30s\n",XX.xingming,XX.xingbie,XX.dianhua,XX.qq,XX.email);
   fclose(fp);
    }


xs()                                 /*显示函数*/
{
    FILE *fp;
    struct xx XX;
    if((fp=fopen("xinxi.txt","r"))==NULL)
      {
        printf("Error....Can't nofind the file!");
        return;
        }
    printf("%13s  %5s%15s%15s%28s\n","xingming","xingbie","dianhua","qq","email");
     while(!feof(fp))
       {
      fputc(fgetc(fp),stdout);
          }
    getch();
    getchar(); /*用来停顿*/
     fclose(fp);
     }
cx()                               /*查询函数*/
{
    struct xx XX;
    FILE *fp;
    int flag;                      /*判断查询条件*/
    char ch[15];
    char tel[13];
    if((fp=fopen("xinxi.txt","r"))==NULL)
      {
     printf("Error....Can't find the file!");
         return;
         }
      printf("(1.name||2.tel)-->[ ]\b\b");
  do
{
  scanf("%d",&flag);
      if(flag==1)                  /*按姓名查询*/
      {
        printf("Please input the name you want to search:");
        scanf("%s",&ch);
    while(!feof(fp))
       {
            fscanf(fp,"%s%s%s%s%s\n",XX.xingming,XX.xingbie,XX.dianhua,XX.qq,XX.email);
        if(strcmp(ch,XX.xingming)==0)
        {
        printf("%13s  %5s%15s%15s%28s\n","xingming","xingbie","dianhua","qq","email");
        printf("%15s%5s%15s%15s%30s\n",XX.xingming,XX.xingbie,XX.dianhua,XX.qq,XX.email);
        getch();
        getchar();
        break;
        }
        }
        printf("%s","not found");
        getch();
        getchar();
        break;
    }
       if(flag==2)                  /*按电话查询*/
         {
           printf("Please input tel you want to search:");
           scanf("%s",&tel);
           while(!feof(fp))
       {
         fscanf(fp,"%s%s%s%s%s\n",XX.xingming,XX.xingbie,XX.dianhua,XX.qq,XX.email);
         if(strcmp(tel,XX.xingming)==0)
        {
        printf("%13s  %5s%15s%15s%28s\n","xingming","xingbie","dianhua","qq","email");
        printf("%15s%5s%15s%15s%30s\n",XX.xingming,XX.xingbie,XX.dianhua,XX.qq,XX.email);
        getch();
        getchar();
        break;
        }
        }
        break;
    }
        printf("%s","not found");
        getch();
        getchar();
        break;
}while(flag<1||flag>2);
               fclose(fp);
 }
xg()                                 /*修改函数*/
{
   struct xx XX;
   FILE *fp1,*fp2;
   char name[15];
   if((fp1=fopen("xinxi.txt","r"))==NULL)
   {
        printf("Error....Can't nofind the file!");
       return;
        }
    if((fp2=fopen("temp.txt","w"))==NULL)
    {
       printf("Error....Can't nofind the file!");
    fclose(fp1);
        return;
     }
        printf("Please input the name you want to modify:");
        scanf("%s",&name);
       while(!feof())
        {
            fscanf(fp,"%s%s%s%s%s\n",XX.xingming,XX.xingbie,XX.dianhua,XX.qq,XX.email);
            if(strcmp(name,XX.xingming)==0)
          {
            printf("\nXingming:%s\n",XX.xingming);
            printf("Xingming  :%s\n",XX.xingbie);
            printf("Xingming  :%s\n",XX.dianhua);
            printf("Xingming  :%s\n",XX.qq);
            printf("Xingming  :%s",XX.email);
            printf("\n\n\nShu Ru Xin Ji Lu\n");
            printf("Xingming:");
            gets(XX.xingming);
            printf("Xingbie:");
            gets(XX.xingbie);
            printf("Dianhua:");
            gets(XX.dianhua);
            printf("QQ:");
            gets(XX.qq);
            printf("E-mail:");
            gets(XX.email);
            }
fprintf(fp2,"%15s%5s%15s%15s%30s\n",XX.xingming,XX.xingbie,XX.dianhua,XX.qq,XX.email);
            }
            fclose(fp1);
            fclose(fp2);
        remove("xinxi.txt");
               rename("temp.txt","xinxi.txt");
}

main()
{
    do
  {
   clrscr();
   switch(printf1())
   {
     case 1:xs();break;
     case 2:tj();break;
     case 3:xg();break;
     case 4:cx();break;
     case 5:create();break;
     case 6:exit(0);break;
     default:printf("Error.....Please perss any key!");break;
     }
  } while(1);
}

2008-07-17 20:24
chengli
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-7-3
收藏
得分:0 
turbo 2.0 编译通过
2008-07-17 20:49
a65931536
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-6-20
收藏
得分:0 
谢谢!吸纳中
2008-07-17 21:47
maiiho
Rank: 1
等 级:新手上路
帖 子:94
专家分:7
注 册:2008-7-17
收藏
得分:0 
看得出楼主是新手
但是chengji你在添加getchar();   /*用来接收'\n'*/的时候,应该也要说明一下为什么要这样,不然楼主很容易学成「刑事犯」的,呵呵
2008-07-17 23:34
快速回复:C语言通讯录,其中有查询函数不有问题,但出在哪里?WIN-TC
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024818 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved