【问题标题】:C Errors (Beginner)C 错误(初学者)
【发布时间】:2009-03-04 06:31:45
【问题描述】:
#include <stdio.h>

main(void) {
file *file = fopen("words.txt","r") 
if(file != null) {
    char line[128];
    while(fgets( line, sizeof line, file) != null) 
    {
    fputs ( line, stdout );
    }
    fclose ( file );
    }
}

这是我的代码。我试图读取文件并输出内容。 但这给了我错误代码

main.c: In function 'main':
main.c:4: error: 'file' undeclared (first use in this function)
main.c:4: error: (Each undeclared identifier is reported only once
main.c:4: error: for each function it appears in.)
main.c:5: error: parse error before "if"
main.c:7: error: 'line' undeclared (first use in this function)
main.c:7: error: 'null' undeclared (first use in this function)
main.c: At top level:
main.c:13: error: parse error before '}' token
main.c:13:2 warning: no newline at end of file

我该如何解决这个错误。

【问题讨论】:

    标签: c


    【解决方案1】:

    FILE 和 NULL 写错了(C 区分大小写)。 fopen 行缺少分号。

    下面的代码编译并运行。

    #include <stdio.h>
    
    main(void) {
    FILE *file = fopen("words.txt","r"); 
    if(file != NULL) {
        char line[128];
        while(fgets( line, sizeof line, file) != NULL) 
        {
        fputs ( line, stdout );
        }
        fclose ( file );
        }
    }
    

    【讨论】:

    • sizeof 应该有括号:sizeof(line)。这种方式更具可读性。
    • 而在 C99 中,main() 应该定义为返回 int。
    • abelenky: sizeof 行是首选,因为 sizeof 是一个运算符,而不是一个函数。括号只需要在类型周围,如“sizeof (int *)”。注意sizeof后面的空格,这也是为了强调sizeof是一个运算符。这确实取决于个人编码标准,但这被广泛使用
    【解决方案2】:

    以下工作正常,并在函数调用和定义以及循环/ifs 中使用一致的大括号和空格进行了清理。如果这是您从标签中的意图,它也可以在 C 中工作,如果文件无法打开,则打印错误并返回 1。

    #include <stdio.h>
    #include <errno.h>
    
    int main (void) {
        char line[128];
        FILE *file = fopen ("words.txt", "r");
        if (file != NULL) {
            while (fgets (line, sizeof line, file) != NULL) {
                fputs (line, stdout);
            }
            fclose (file);
        } else {
            fprintf (stderr, "Cannot open 'words.txt', error = %d\n", errno);
            return 1;
        }
        return 0;
    }
    

    【讨论】:

    • “Cuddled elses”(即一行中的“} else {”)对某些人来说是不好的风格,但我也喜欢它们。
    • 我喜欢同时在屏幕上看到尽可能多的代码,所以在同一行打开大括号就像 if/while 和“拥抱 elses”(这是我)更可取。
    【解决方案3】:

    尝试将FILE 大写。

    【讨论】:

      【解决方案4】:

      除了已经指出的其他内容,您的main() 函数应指定为返回int,并应在最后明确这样做。另外,我不太了解您的缩进和括号样式。

      #include <stdio.h>
      
      int main(void)
      {
        FILE *file = fopen("words.txt","r"); 
        if(file != NULL)
        {
          char line[128];
          while(fgets( line, sizeof line, file) != NULL) 
          {
            fputs ( line, stdout );
          }
          fclose ( file );
        }
        return 0;
      }
      

      或者,如果文件未打开,您可以设置为 return 1;(在 UNIX 上错误代码通常为非零):

      #include <stdio.h>
      
      int main(void)
      {
        FILE *file = fopen("words.txt","r"); 
        if(file != NULL)
        {
          char line[128];
          while(fgets( line, sizeof line, file) != NULL) 
          {
            fputs ( line, stdout );
          }
          fclose ( file );
          return 0;
        }
        else
        {
          return 1;
        }
      }
      

      【讨论】:

        【解决方案5】:
        • 文件应在第 4 行开头大写
        • 第 4 行末尾缺少分号,导致第 5 行出现解析问题

        编辑了两次,因为我编辑错了。哎呀。

        【讨论】:

        • sizeof 是一个运算符,而不是一个函数。
        • 是的,我只是仔细检查了一遍;我的错。
        • sizeof 在这里不需要括号,因为 `line' 是一个变量。变量或表达式不需要括号,只需要类型名称,AFAIK。
        • 是的,再一次,我的内存完全失败了。
        【解决方案6】:

        文件类型在 stdio.h 中定义并且必须是全大写 (FILE)。犯简单的错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-17
          • 2017-09-20
          • 2011-02-24
          • 1970-01-01
          相关资源
          最近更新 更多