qseatnum.cpp

来自「内有说明」· C++ 代码 · 共 66 行

CPP
66
字号
//num 按学号查找学生信息
#include "qseatnum.h"

void QueryBySeatNum()
{
  int result = 0;
  Student Tmps;
  int number;
  char DataFile[40] = "", next;
  FILE *fp = NULL;

  //提示教师输入要查询的文件名
  printf("\nplease inout the name of file where data is stored,end with enter key.\n");
  gets(DataFile);

  //提示教师输入要查询的文件名
  while (*DataFile == ('\0'))
  {
    printf("\nplease inout the name of file where data is stored,end with enter key.\n");
    gets(DataFile);
  }

  //提示输入要查询学生的学号
begin:
  result = 0;
  printf("please input the num of employee which needs look up(under 20 bit figure),end with enter key.\n");
  scanf("%d", &number);
  getchar();

  //以读的方式打开文件
  fp = fopen(DataFile, "r");
  if (fp == NULL)
  {
    printf("\nopenfile %s fail ! end with any key .\n", DataFile);
    perror("open file fail");
    getch();
    exit(1);
  }

  //循环查找和输入学号匹配的学生信息,如果查找到,则输出结果
  while (feof(fp) == 0)
  {
    if (fread(&Tmps, sizeof(Student), 1, fp))
    {
      if (Tmps.Number = number)
      {
        printf("\nfind:)\n");
        printf("\nnumber=%ld   name=%s   chinesescore=%5.2f   mathscore=%5.2f   heji=%5.2f\n", Tmps.Number, Tmps.Name, Tmps.chinesescore, Tmps.mathscore, Tmps.heji);
        result = 1; // 说明找到了该学生的对应信息
      }
    }
  }
  fclose(fp);

  //提示教师已经查找到了结果并询问是否继续查找
  if (result == 0)
  {
    printf("there is not data of this Student in the file!");
  }
  printf("\ngo on? (y/n)");
  next = getche();
  putchar('\n');
  if (next == 'y' || next == 'Y')
    goto begin;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?