【问题标题】:Why does this code only print the first line of the file?为什么这段代码只打印文件的第一行?
【发布时间】:2021-05-24 05:38:38
【问题描述】:

要求:

编写一个按顺序执行以下任务的 C 程序:

  • 在执行窗口中初始显示文件内容
  • 向用户询问 ID,在文件中搜索 ID,然后以以下格式显示消息(在执行窗口中):学生 ID:…………对应于………………。谁主修……………… 当您在输出中打印学生姓名时,请勿打印“_”!

问题: 它只输出文件的第一行(也就是第一个学生信息),之后甚至不询问学生 ID。

代码:

#include <stdio.h>
#include <string.h>
typedef struct {
    int ID;
    char Last_name[30], First_name[30], major[30];
} Class;
int search_for_student(int ID_to_search, Class students[22]) {
    int i;
    for (i = 0; i < 22; i++) {
        if (students[i].ID == ID_to_search)
            return i;
    }
    return -101;
}
void main(void) {
    char line[100];
    int  ID_to_search, i, index, j;
    Class students[22];
    FILE* infp;
    infp = fopen("Section_06.txt", "r");
    if (infp == NULL) {
        printf("File unexistant!\n");
    }
    else {
        for (i = 0; i < 22; i++) {
            fscanf(infp, "%d", students[i].ID);
            printf("%d", students[i].ID);
            fscanf(infp, "%s", students[i].Last_name);
            for (j = 0; j < strlen(students[i].Last_name); j++) {
                if (students[i].Last_name[j] == '_')
                    students[i].Last_name[j] = ' ';
            }
            printf("%s", students[i].Last_name);
            fscanf(infp, "%s", students[i].First_name);
            for (j = 0; j < strlen(students[i].First_name); j++) {
                if (students[i].First_name[j] == '_')
                    students[i].First_name[j] = ' ';
            }
            printf("%s", students[i].First_name);
            fgets(students[i].major, 30, infp);
            printf("%s", students[i].major);
        }
        printf("Enter an ID: ");
        scanf("%d", &ID_to_search);
        index = search_for_student(ID_to_search, students);
        if (index == -101)
            printf("Student not found!");
        else {
            printf("Student with ID: %d corresponds to %s %s who is majoring in %s.", students[index].ID, students[index].First_name, students[index].Last_name, students[index].major);
        }
    }
    fclose(infp);
}

【问题讨论】:

  • 编辑您的问题并在此处的单独代码块中发布一些具有代表性的输入数据。
  • 我将从学习一种有用的方法来读取文件开始。有代表性的例子见here
  • 关于:void main(void) { 如果你的编译器允许这个语句,那么它就是一个非编译编译器! main() 只有两个有效签名,它们是:int main( void )int main( int argc, char *argv[] )
  • 关于如下语句:for (j = 0; j &lt; strlen(students[i].Last_name); j++) { 这是将无符号值与有符号值进行比较。 (jint 和函数:strlen() 返回一个无符号的 size_t)这仅适用于小的正值
  • OT:发布的代码包含几个“神奇”数字:I.E. 22、30、100。“神奇”数字使代码更难理解、调试等。建议使用#define 语句或enum 语句为这些“神奇”数字赋予有意义的名称,然后使用那些有意义的名称贯穿整个代码。

标签: c file structure


【解决方案1】:

有几点:

  1. main 应该返回 int

  2. fscanf(info, "%d", &students[I].ID); // '&' 被遗漏了。

  3. 文件读取循环

    对于 (i = 0; i

不好。

while(fgets(buff, sizeof(buff), infp)!=NULL){
}

应该会更好。该文件可能有更多或更少的行;

  1. 将 fscanf 与 get 结合起来并不好。请使用 fgets 读取 line 和 sscanf 或 strtok 获取字段。

【讨论】:

    【解决方案2】:

    您在此行错过了&amp;

    fscanf(infp, "%d", students[i].ID);
    

    改为:

    fscanf(infp, "%d", &students[i].ID);
    

    【讨论】:

      【解决方案3】:

      关于:

      问题:它只输出文件的第一行(即第一个学生信息),之后甚至不要求学生 ID。

      感兴趣的代码是

          printf("Enter an ID: ");
          scanf("%d", &ID_to_search);
          index = search_for_student(ID_to_search, students);
          
          if (index == -101)
              printf("Student not found!");
          else {
              printf("Student with ID: %d corresponds to %s %s who is majoring in %s.", students[index].ID, students[index].First_name, students[index].Last_name, students[index].major);
          } 
      }  // end if/else, not the end of a `for() loop
      

      请注意,上面的代码是“else”代码块的结尾,不是for()循环的一部分,所以只会执行一次

      此外,如果要输入的学生少于 22 人,则对 fscanf() 的调用将返回 EOF,而不是更新任何学生信息。您确定输入文件实际上包含 22 个学生信息实例吗?它甚至包含超过 1 个学生信息实例吗?

      而不是使用硬编码的计数 22,用于:

      for (i = 0; i < 22; i++) 
          {
              fscanf(infp, "%d", students[i].ID);
      

      建议使用第一次调用 fscanf() 时返回的值,类似于:

      i = 0;
      while( fscanf(infp, "%d", students[i].ID) != EOF )
      {
          ....
          i++;
      } 
      

      【讨论】:

        猜你喜欢
        • 2019-11-17
        • 1970-01-01
        • 1970-01-01
        • 2018-07-01
        • 2017-05-08
        • 1970-01-01
        • 2015-12-17
        • 1970-01-01
        • 2023-03-25
        相关资源
        最近更新 更多