| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1362 人关注过本帖
标题:学生信息查询
只看楼主 加入收藏
选调生
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:82
专家分:100
注 册:2016-6-24
结帖率:66.67%
收藏
已结贴  问题点数:18 回复次数:5 
学生信息查询
这是学生管理的查找部分,运行有误,我想通过查找学生学号来查找信息,亲们,帮忙改改吧。


//查找学生信息  
void search()
{
    FILE *fp;
        int Y;
    int num;
    struct student *p;
   
    p=(struct student *)malloc(sizeof(struct student));
    fp=fopen("super.txt","r");
    //p2=(struct student *)malloc(sizeof(struct student));
    printf("请输入要查找的学生号:  ");
    scanf("%d",&num);
        if(Y==num)
    {
    while(fread(p,sizeof(struct student),1,fp)!=NULL)
    {
          printf("%d,%s,%s,%s,%s,%s,%s,%s\n",p->num,p->clas,p->name,p->sex,p->item,p->phonenum,p->area,p->strip);
          //p=(struct student *)malloc(sizeof(struct student));
    }
    }
    system("pause");
    fclose(fp);
}

[此贴子已经被作者于2016-6-30 10:31编辑过]

搜索更多相关主题的帖子: search 信息 
2016-06-30 10:09
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:9 
Y是个什么鬼?
看你代码的意思应该是这样:
程序代码:
void search()
{
    FILE *fp;
    int Y;
    int num;
    struct student *p;

    p=(struct student *)malloc(sizeof(struct student));
    fp=fopen("super.txt","r");
    //p2=(struct student *)malloc(sizeof(struct student));
    printf("ÇëÊäÈëÒª²éÕÒµÄѧÉúºÅ:  ");
    scanf("%d",&num);

 
    while(fread(p,sizeof(struct student),1,fp)!=NULL)
    {
        if(p->num == num)
        {
           printf("%d,%s,%s,%s,%s,%s,%s,%s\n",p->num,p->clas,p->name,p->sex,p->item,p->phonenum,p->area,p->strip);
           //p=(struct student *)malloc(sizeof(struct student));
        }
    }
    system("pause");
    fclose(fp);
}
2016-06-30 10:35
选调生
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:82
专家分:100
注 册:2016-6-24
收藏
得分:0 
c.rar (30.53 KB)
  这个是录入信息

未命cjk.rar (45.11 KB)
  这个是输出信息

查找.rar (45.49 KB)
  这个是查找信息

我只想查询2号学生,但是查找结果是2名同学都显示

2016-06-30 10:42
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
p->num是学生个数?那学号存在哪里
2016-06-30 10:58
选调生
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:82
专家分:100
注 册:2016-6-24
收藏
得分:0 
回复 4楼 grmmylbs
我把代码发上去吧。


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define N 20
#define M 30
void output();
void search();
void insert(struct student *h);
void delet();
void modify(struct student *p);
void rank();               //排序
void statics();            //统计
void save(struct student *head);
//学生信息
struct student
{
    int num;               //学生号
    char clas[M];         //学生班级
    char name[N];
    char sex[N];          //性别
    char item[N];         //项目
    char phonenum[N];     //学生电话
    char area[N];         //领取号码条区
    char strip[N];        //号码条
    struct student *next;
};

//录入学生信息        //////改过  
struct student *input()
{
    FILE *fp;

    int i,n;
    struct student *p,*p1,*p2,*head;
    p2=(struct student *)malloc(sizeof(struct student));
    head=p1=(struct student *)malloc(sizeof(struct student));
    i=1;
    printf("录入学生数n: ");
    scanf("%d",&n);
    printf("请输入参赛学生信息:\n学生号\t 学生班级\t   姓名\t性别\t项目\t 学生电话\t 领码区\t 号码条\n\n");
   
    scanf("%d%s%s%s%s%s%s%s",&p2->num,p2->clas,p2->name,p2->sex,p2->item,p2->phonenum,p2->area,p2->strip);
    p2->next=NULL;
    p1->next=p2;
    p1=p2;
    while(i<n)
    {                           //p1的下一结点只向新结点p2,p1始终指向最后的结点
        p2=(struct student *)malloc(sizeof(struct student));         //为新结点申请空间
      
        scanf("%d%s%s%s%s%s%s%s",&p2->num,p2->clas,p2->name,p2->sex,p2->item,p2->phonenum,p2->area,p2->strip);
        p1->next=p2;
        p2->next=NULL;
        p1=p2;
        i++;
    }
    //p1->next=NULL;                         //使p1下一结点指向NULL
    fp=fopen("super.txt","w");
    p=head->next;
    while(p!=NULL)
    {
        //fprintf(fp,"%d %s %s %s %s %s %s %s",p->num,p->clas,p->name,p->sex,p->item,p->phonenum,p->area,p->strip);
        fwrite(p,sizeof(struct student),1,fp);
        p=p->next;
    }
    fclose(fp);
    p=head->next;
    while(p!=NULL)
    {
         printf("%d\n",p->num);
         p=p->next;
    }
    system("pause");
    return head;
}


//主函数     
main()
{
    int i,x,j;
    int flag=0,Y=0;
    FILE *fp;
    struct student * head;
    char pw[N],land[N];
    struct secret p1,p2;
    while(1)
    {
        printf("\n\t\t\t|---◆  !注册用户名按1号键!  ◆---|\n\n");
                     printf("\t\t\t|---◆   !用户登录按2号键!   ◆---|\n\n");
                     printf("\t\t\t|---◆ !浏览所有信息按3号键! ◆---|\n\n");
                     printf("请输入您要操作的编号:  ");
        scanf("%d",&flag);
        if(flag==1)
        {
            printf("\n请输入要注册的用户名:\t");
            scanf("%s",p1.admin);
            fp=fopen("pass.txt","a+");
            printf("\n请输入要设置的6位密码:\t");
            scanf("%s",p1.password);
            for(i=0;i<6;i++)
            {
                p1.password[i]=p1.password[i]+4;                  //输入密码
            }
            fprintf(fp,"%s %s\n",p1.admin,p1.password);
            fclose(fp);
        }
        ////////////////////
        if(flag==2)
        {
             for(i=0;i<3;i++)
             {
                 printf("请输入用户名:\t");
                 scanf("%s",p2.admin);
                 printf("\n\n                            欢迎来到运动会管理系统\n请输入您的6位密码:\t                      \n");

    /////////////////////////////////密码再次输入有问题!/////////////////////////////////
                 for(j=0;j<6;j++)
                 {
                        land[j]=getch();                        //输入密码
                     printf("*");
                 }
                 for(j=0;j<flag;j++)
                     land[j]=land[j]+4;
         
            strcpy(p2.password,land);
            fp=fopen("pass.txt","r");
     
            while(fscanf(fp,"%s %s",p1.admin,p1.password)!=EOF)         //EOF符号结束读取文件
            {  
                if((strcmp(p1.admin,p2.admin)==0)&&(strcmp(p1.password,p2.password)==0))
                {
                    Y=1;
                    break;
                }
         
            }

           if(Y=1)
      
           {
               for(i=0;i<6;i++)
               {
                  printf("\n\n            *********** ♀♀欢迎进入运动会管理系统♀♀ ***********                   \n");
                  printf("                *             按1号键:录入学生信息。                *                         \n");
                  printf("                *             按2号键:输出学生信息。                *                         \n");
                  printf("                *             按3号键:修改学生信息。                *                                   \n");
                  printf("                *             按4号键:删除学生信息。                *                                  \n");
                  printf("                *             按5号键:查找学生信息。                *                                   \n");
                  printf("                *             按6号键:添加学生信息。                *                                  \n");
                  printf("                *             按7号键:排列学生信息。                *                                   \n");
                  printf("                *             按8号键:统计学生信息。                *                                   \n");
                  printf("                *             按9号键:保存信息。                    *                         \n");
                  printf("                *             按0号键:退出系统。                    *                         \n");
                  printf("                ******************************************************                        \n");
  
   
                  printf("\n\n请输入您要做的操作编号:\t");
                  scanf("%d",&x);
                  if(x==0) break;
                    head=(struct student *)malloc(sizeof(struct student));
                  switch(x)
                  {
                     case 1: head=input();
                             system("cls");                //清屏
                             break;
                     case 2: output(head);
                             break;                           
                     case 3: modify(head);
                             break;
                     case 4: delet();
                             break;
                     case 5: search();
                             break;
                     case 6: insert(head);
                             break;
                     case 7: rank();
                             break;
                     case 8: statics();
                             break;
                     case 9:save(head);
                             break;
                  }
               }
      
                 break;   
  
           }
      
      
           else  
               printf("对不起您的密码有误\n");
        }
    }                               //if(flag==2)结束   

    if(flag==3)
        rank();

  }
}
   



//输出学生信息       //////改过
void output()
{
    FILE *fp;
    struct student *p;
    p=(struct student *)malloc(sizeof(struct student));
   
    fp=fopen("super.txt","r");
    while(fread(p,sizeof(struct student),1,fp)!=NULL)
    {
          printf("%d,%s,%s,%s,%s,%s,%s,%s\n",p->num,p->clas,p->name,p->sex,p->item,p->phonenum,p->area,p->strip);
          //p=(struct student *)malloc(sizeof(struct student));
    }
    system("pause");
    fclose(fp);
}

//查找学生信息  
void search()
{
    FILE *fp;
   // char r[N];
    int num;
    struct student *p;
   
    p=(struct student *)malloc(sizeof(struct student));
    fp=fopen("super.txt","r");
    //p2=(struct student *)malloc(sizeof(struct student));
    printf("请输入要查找的学生号:  ");
    scanf("%d",&num);

    while(fread(p,sizeof(struct student),1,fp)!=NULL)
    {
          printf("%d,%s,%s,%s,%s,%s,%s,%s\n",p->num,p->clas,p->name,p->sex,p->item,p->phonenum,p->area,p->strip);
          //p=(struct student *)malloc(sizeof(struct student));
    }
    system("pause");
    fclose(fp);
}
2016-06-30 11:34
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:9 
看他的程序好像没有将文件里的信息读取出来,而是直接在文件里寻找相对应的信息。
如果是这样的话,那么查找的学号就不应该用int num;这个类型,而是要改成字符串,类似char num[10];
然后用strcmp比较。
2016-06-30 11:40
快速回复:学生信息查询
数据加载中...
 
   



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

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