簡體   English   中英

你能解釋一下為什么函數文件*在 C 中不起作用嗎?

[英]can u explain me why function file* doesnt work in C?

我有很多錯誤,我不知道為什么會這樣? 我想創建文件movies.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);

附注。 如何在功能切換案例中將此 data.c 連接到 main.c?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM