Изучаю C, в очередной раз переписал код из книги. Выдало множество ошибок - C (СИ)
Формулировка задачи:
Здравствуйте! Изучаю C. Это мой первый язык программирования. В книге авторы для компиляции советовали использовать gcc. Так как я знаком с Linux Ubuntu, то воспользовался предустановленной там версией gcc ничего не меняя. Всё шло хорошо и не возникало проблем. Но в один момент при изучении потоков данных я переписал код из книги, скомпилировал и получил множество ошибок. Объясните, пожалуйста, что я делаю не так. Код и сообщения об ошибках прилагаются.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char line[80];
FILE *in = fopen ("spooky.csv", "r");
FILE *file1 = fopen ("ufos.csv", "w");
FILE *file2 = fopen ("disappireance.csv", "w");
FILE *file3 = fopen ("others.csv", "w");
while (fscanf (in, "%79[^\n]\n", line) == 1) {
if (strstr (line, "НЛО"))
fprintf(file1, "%s\n", line);
else if (strstr(line, "Исчезновение"))
fprintf (file2, "%s\n", line);
else
fprintf (file3, "%s\n", line);
}
fclose(file1);
fclose(file2);
fclose(file3);
return 0;
}dagon@DagonPC:~/Документы$ gcc categorize.c -o categorize && ./categorize
categorize.c: In function ‘main’:
categorize.c:10:15: error: invalid initializer
FILE file1 = fopen ("ufos.csv", "w");
^
categorize.c:11:15: error: invalid initializer
FILE file2 = fopen ("disappireance.csv", "w");
^
categorize.c:12:15: error: invalid initializer
FILE file3 = fopen ("others.csv", "w");
^
categorize.c:16:12: error: incompatible type for argument 1 of ‘fprintf’
fprintf(file1, "%s\n", line);
^
In file included from categorize.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘FILE {aka struct _IO_FILE}’
extern int fprintf (FILE *__restrict __stream,
^
categorize.c:18:13: error: incompatible type for argument 1 of ‘fprintf’
fprintf (file2, "%s\n", line);
^
In file included from categorize.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘FILE {aka struct _IO_FILE}’
extern int fprintf (FILE *__restrict __stream,
^
categorize.c:20:13: error: incompatible type for argument 1 of ‘fprintf’
fprintf (file3, "%s\n", line);
^
In file included from categorize.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘FILE {aka struct _IO_FILE}’
extern int fprintf (FILE *__restrict __stream,
^
categorize.c:23:9: error: incompatible type for argument 1 of ‘fclose’
fclose(file1);
^
In file included from categorize.c:1:0:
/usr/include/stdio.h:237:12: note: expected ‘FILE * {aka struct _IO_FILE *}’ but argument is of type ‘FILE {aka struct _IO_FILE}’
extern int fclose (FILE *__stream);
^
categorize.c:24:9: error: incompatible type for argument 1 of ‘fclose’
fclose(file2);
^
In file included from categorize.c:1:0:
/usr/include/stdio.h:237:12: note: expected ‘FILE * {aka struct _IO_FILE *}’ but argument is of type ‘FILE {aka struct _IO_FILE}’
extern int fclose (FILE *__stream);
^
categorize.c:25:9: error: incompatible type for argument 1 of ‘fclose’
fclose(file3);
^
In file included from categorize.c:1:0:
/usr/include/stdio.h:237:12: note: expected ‘FILE * {aka struct _IO_FILE *}’ but argument is of type ‘FILE {aka struct _IO_FILE}’
extern int fclose (FILE *__stream);
^
Решение задачи: «Изучаю C, в очередной раз переписал код из книги. Выдало множество ошибок»
textual
Листинг программы
FILE* file1 = fopen ("ufos.csv", "w");
Объяснение кода листинга программы
- В коде открывается файл
ufos.csvдля записи (режимw). - Переменная file1 получает дескриптор файла.