| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 406 人关注过本帖
标题:高手看看哪儿出错了 通讯录代码
只看楼主 加入收藏
tianxintong
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-3-30
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:2 
高手看看哪儿出错了 通讯录代码
这通讯录代码 编译没错   查询时无论怎么输入都显示的是第一行的内容
修改函数修改不了内容  删除函数只能删除姓名 电话 QQ之类的 还是保留着
求高手指教  
 不懂的勿扰




#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
struct student
{
    char xingming[45];
    char xingbie[5];
    char dianhua[13];
    char qq[13];
    char email[30];

}xx,XX;
int printf1()
{
    int i;
    printf("---------------------------------通讯录");
    printf("----------------------------------\n");
    printf("                                 1显示\n");
    printf("                                 2添加\n");
    printf("                                 3修改\n");
       printf("                                 4查找\n");
       printf("                                 5删除\n");
       printf("                                 6退出\n");
       printf("我要选择:");
       scanf("%d",&i);return i;
}
add()
{
    int a;

    FILE *fp;
    if((fp=fopen("xinxi.txt","a+"))==NULL)
{
    printf("error....\n");
    getch();
}
printf("输入姓名:");
gets(xx.xingming);
system("cls");
printf("输入姓名:");
gets(xx.xingming);
printf("输入性别:");
gets(xx.xingbie);
printf("输入电话:");
gets(xx.dianhua);
printf("输入QQ:");
gets(xx.qq);
printf("输入eiail:");
gets(xx.email);
printf("\n1.返回--2.添加--3.退出  4baocun");
scanf("%d",&a);
if(a==1)
{
     main();
}
else if(a==2)
     {
          add();
     }
     else if(a==3)
          {
              exit(0);
          }
          else if(a==4)
                {
                    save();
                }
         
}
show()
{
    int i;
    FILE *fp;
    if((fp=fopen("xinxi.txt","rb"))==NULL)
{
    printf("error....\n");
    getch();
    main();
}

     while(fread(&xx,sizeof(xx),1,fp)==1)
     {
         printf("姓名:%s\n",xx.xingming);
         printf("性别:%s\n",xx.xingbie);
         printf("电话:%s\n",xx.dianhua);
         printf("qq:%s\n",xx.qq);
         printf("email:%s\n",xx.email);
     }
 
     fclose(fp);
     printf("1.返回  2.退出");
     scanf("%d",&i);
     if(i==1)
     {
         main();
     }
     else if(i==2)
     {
         exit(0);
     }
          else
          {
              getch();
              show();
          }
}
update()
{     
   FILE *fp;   
   int m,k=0;
  long offset;
  printf("\n输入姓名:");
  scanf("%s",XX.xingming);
fp=fopen("xinxi.txt","r+");
     while(!feof(fp))
     {   
      offset=ftell(fp);
      fscanf(fp,"%-11s%-10s%-10s%-15s%-15s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
      if(!strcmp(XX.xingming,xx.xingming))
       {
        k=1;
        break;   
       }
     }
        if(k)
        {   
         printf("\n记录为:\n");
         printf("\n%-11s%-10s%-10s%-15s%-15s","xingming","xingbie","dianhua","qq","email");
printf("\n%-11s%-10s%-10s%-15s%-15s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
         printf("\n输入新姓名 性别 电话 qq email:");
     scanf("%-11s%-10s%-10s%-15s%-15s",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
         fseek(fp,offset,SEEK_SET);
         printf("%ld",ftell(fp));
         fprintf(fp,"%-11s%-10s%-10s%-15s%-15s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
        }
         else
         printf("\n没此人。");
         fclose(fp);     
}
search()
{
    FILE *fp;
    int k=0;
  printf("\n输入姓名:");
  scanf("%s",XX.xingming);
 fp=fopen("xinxi.txt","rb");
    while(!feof(fp)) /*文件检测未结束时进行循环*/
     {   
        fscanf(fp,"%s%s%s%s%s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
      if(strcmp(XX.xingming,xx.xingming)) /*输入的姓名和已有的比较*/
       {
        printf("\n\n记录为:\n");
       printf("%-11s%-10s%-10s%-15s%-15s\n","xingming","xingbie","dianhua","qq","email");
printf("\n%-11s%-10s%-10s%-15s%-15s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
        k=1;
       }
     }
        if(!k)   
        printf("\n\n没人。");
        fclose(fp);  
   
      
}
del()
{
    FILE *fp;
   int m,k=0,flag;
   long offset1;
   char  valid[4];
   printf("\n请输入姓名:");
   scanf("%s",XX.xingming);
fp=fopen("xinxi.txt","r+");
      while(!feof(fp))
      {   
       offset1=ftell(fp);         
fscanf(fp,"%s%s%s%s%s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
       if(!strcmp(XX.xingming,xx.xingming))
        {   
         k=1;
         break;  
        }
      }
         if(k)
          {  
           printf("\n查到的记录为:");
           printf("\n%-11s%-10s%-10s%-15s%-15s\n","姓名","性别","电话","qq","email");
    printf("%-11s%-10s%-10s%-15s%-15s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);  
           printf("\n删除按1,不删按0:");
           scanf("%d",&m);
           if(m)
           {   
             fseek(fp,offset1,SEEK_SET);/*将fp所指向设置到offset1*/
                    fprintf(fp,"%-11s%-10s%-16s%-12s\n","","","",""); //为字符块的和
            }
          }   
 else  printf("\n对不起,通讯簿中没有此人的记录。");
            fclose(fp);
}
save()
{
    FILE *fp;
    int i;
    if((fp=fopen("xinxi.txt","a+"))==NULL)
    {
        printf("error");
        return;
    }
    if(fwrite(&xx,sizeof(struct student),1,fp)!=1)
    printf("file write error");
    fclose(fp);
    main();
}
main()
{
    switch(printf1()){
    case 1:show();break;
    case 2:add();break;
    case 3:update();break;
    case 4:search();break;
    case 5:del();break;
    case 6:exit(0);
    }
}
搜索更多相关主题的帖子: 通讯录 email 姓名 
2011-04-05 16:05
变幻小子
Rank: 6Rank: 6
来 自:广东陆丰
等 级:侠之大者
帖 子:188
专家分:473
注 册:2011-3-4
收藏
得分:5 
路过学习

明天的梦
2011-04-05 18:19
bccn250
Rank: 5Rank: 5
等 级:职业侠客
帖 子:154
专家分:379
注 册:2010-5-25
收藏
得分:5 
首先,先向路过的那人学习

然后我想问问问题,楼主是看什么书学的??
我第一次看到直接调用 main 的用法,哪本书,我也想看看
还有就是,楼主用的是什么编译器,我自己用的是 VC6.0(有点老了),编译不能通过

我真的没见过这种用法,哪本书上有讲,麻烦知道的人,推荐一下
2011-04-05 18:43
快速回复:高手看看哪儿出错了 通讯录代码
数据加载中...
 
   



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

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