代码之家  ›  专栏  ›  技术社区  ›  Jordan Davis

在C中打开文件[已关闭]

  •  -5
  • Jordan Davis  · 技术社区  · 10 年前

    在我写这篇文章之前,我看了很多例子,我知道很多语言,清楚地理解了下面列出的代码中的语法和内容,但是,当我编译我的程序时,例如:

    gcc -o /sbin/"name" readfile.c

    我收到以下错误:

    enter image description here

    这对我来说毫无意义,因为我的代码清楚地包括 #include <stdio.h> 它定义了 FILE 如本文所述---- stdio.h .

    //程序(readfile.c)

    #include <stdio.h>
    
    int main(){
        FILE *fp;
        fp = fopen("dummy.txt","w");
        fprintf(fp, "testing...\n");
        fclose(fp);
    }
    

    //FILE(dumm.txt)*位于同一目录中

    Hello World
    
    1 回复  |  直到 10 年前
        1
  •  3
  •   ameyCU    10 年前

    只是一些小错误

     FILE *fp                     // semi-colon required
     fprint(fp, "testing...\n";   // Its fprintf and missing ')'
     flcose(fp);                 // fclose  is function 
    

    函数为 fprintf fprint 而且 fclose flcose .

    注意-您应该检查 fopen