您能解释一下为什么功能文件*在C中不起作用吗?

时间:2018-12-20 19:00:13

标签: c file debugging

我有很多错误,我不知道为什么会这样? 我想创建文件movie.txt并写一些想法。当我尝试构建此程序时,我无法。

||=== Build: Debug in projekt nr 6 (compiler: GNU GCC Compiler) ===|
XXX\...\|6|error: unknown type name 'FILE'|
XXX\...\|7|warning: data definition has no type or storage class|
XXX\...\|7|warning: type defaults to 'int' in declaration of 'plik' [-Wimplicit-int]|
XXX\...\|7|error: conflicting types for 'plik'|
XXX\...\|6|note: previous declaration of 'plik' was here|
XXX\...\|7|warning: implicit declaration of function 'fopen' [-Wimplicit-function-declaration]|
XXX\...\|7|error: initializer element is not constant|
XXX\...\|8|error: expected identifier or '(' before 'if'|
XXX\...\|14|error: expected declaration specifiers or '...' before string constant|
XXX\...\|15|error: expected declaration specifiers or '...' before string constant|
XXX\...\|15|error: expected declaration specifiers or '...' before '&' token|
XXX\...\|17|error: expected declaration specifiers or '...' before string constant|
XXX\...\|18|error: expected declaration specifiers or '...' before string constant|
XXX\...\main.c||In function 'main':|
XXX\...\main.c|45|error: expected identifier or '*' before '(' token|
||=== Build failed: 10 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
XXX\...\|18|error: expected declaration specifiers or '...' before '&' token|
XXX\...\|20|error: expected declaration specifiers or '...' before string constant|
XXX\...\|21|error: expected declaration specifiers or '...' before string constant|
XXX\...\|21|error: expected declaration specifiers or '...' before '&' token|
XXX\...\|23|error: expected ')' before string constant|
XXX\...\|24|warning: data definition has no type or storage class|
XXX\...\|24|warning: type defaults to 'int' in declaration of 'fclose' [-Wimplicit-int]|
XXX\...\|24|warning: parameter names (without types) in function declaration|
||=== Build failed: 15 error(s), 6 warning(s) (0 minute(s), 0 second(s)) ===|

这是我的代码data.c(不是main.c):

   char title, genre;
int year;

FILE *plik;
plik=fopen("movies.txt", "a");
if(plik == NULL)
   {
   perror("Błąd otwarcia pliku");
   exit(-10);
   }

printf("Podaj nazwe filmu: ");
scanf("%s", &title);

printf("Podaj rok produkcji: ");
scanf("%s", &year);

printf("Podaj gatunek filmu: ");
scanf("%s", &genre);

fprintf(plik,"%s %s %d", title,genre,year);
fclose(plik);

PS。在功能开关的情况下,如何将此data.c连接到main.c?

1 个答案:

答案 0 :(得分:0)

FILE *在C语言中可以正常工作,但是您的代码必须有效。从评论中可以清楚地看出,您不是。没有任何一种编程语言可以很好地猜测它的使用方式,但是如果您选择采用这种方法,那么C是一个特别糟糕的选择,因为它非常无情。

这是显示代码的方法。注意,我包括所有相关代码,仅包含相关代码,显示了我如何构建它,如何调用它以及运行时发生了什么。如果我只显示main()中的部分,那就不会一样了。

$ cat t.c ; gcc -o t2 t.c && { ./t2; ./t2; ./t2; cat movies.txt;}

#include <stdio.h>
#include <unistd.h>

int main(void){
  FILE* f = fopen("movies.txt", "a");
  if( f == NULL) {
      perror("Couldn't open file");
      return 1;
  }
  fprintf(f, "Wrote to file from PID %d\n", getpid());
}

Wrote to file from PID 71729
Wrote to file from PID 71741
Wrote to file from PID 71747
Wrote to file from PID 71767
Wrote to file from PID 71782
Wrote to file from PID 71783
Wrote to file from PID 71784