错误:未知类型名称'FILE' [英] error: unknown type name ‘FILE’
问题描述
我正在制作一个函数,只是将hello写入文件。我把它放在一个不同的文件中,并将它的头文件包含在程序中。但gcc发出错误,即:错误:未知类型名称'FILE'。
代码如下:
app.c:
#包括< stdio.h中>
#include< stdlib.h>
#includewrite_hello.h
int main(){
FILE * fp;
fp = fopen(new_file.txt,w);
write_hello(fp);
返回0;
}
write_hello.h:
void write_hello(FILE *);
write_hello.c:
void write_hello(FILE * fp){
fprintf(fp,hello);
printf(Done \\\
);
}
当由gcc编译时,会发生以下情况:
harsh @ harsh-Inspiron-3558:〜/ c / bank_management / include / test $ sudo gcc app.c
write_hello.c -o app
write_hello.c:3:18:error:unknown type name'FILE'
void write_hello(FILE * fp){
^
对不起,有任何错误。我是初学者。
FILE在stdio.h中定义,您需要将其包含在每个使用它的文件中。所以write_hello.h和write_hello.c都应该包含它,write_hello.c也应该包含write_hello.h(因为它实现了write_hello.h中定义的函数)。
还要注意,每个头文件的标准做法是定义一个名称相同的宏(IN CAPS),并在#ifndef,#endif之间包含整个头文件。在C中,这可以防止头部获得#included两次。这被称为内部包含警卫(感谢Story Teller指出)。
write_hello.h
#ifndef WRITE_HELLO_H
#define WRITE_HELLO_H
#include< stdio.h>
void write_hello(FILE *);
#endif
write_hello.c
#include
#includewrite_hello.h
void write_hello(FILE * fp){
fprintf(fp,hello);
printf(Done \\\
);
}
I am making a function which is just writes "hello" to a file. I have put it in a different file and included its header in the program.But gcc is giving an error namely: error: unknown type name ‘FILE’. The code is given below
app.c:
#include<stdio.h>
#include<stdlib.h>
#include"write_hello.h"
int main(){
FILE* fp;
fp = fopen("new_file.txt","w");
write_hello(fp);
return 0;
}
write_hello.h:
void write_hello(FILE*);
write_hello.c:
void write_hello(FILE* fp){
fprintf(fp,"hello");
printf("Done\n");
}
when compiling by gcc the following occurs:
harsh@harsh-Inspiron-3558:~/c/bank_management/include/test$ sudo gcc app.c
write_hello.c -o app
write_hello.c:3:18: error: unknown type name ‘FILE’
void write_hello(FILE* fp){
^
Sorry for any mistakes.I am a beginner.
FILE is defined in stdio.h and you need to include it in each file that uses it. So write_hello.h and write_hello.c should both include it, and write_hello.c should also include write_hello.h (since it implements the function defined in write_hello.h).
Also note that it is standard practice for every header file to define a macro of the same name (IN CAPS), and enclose the entire header between #ifndef, #endif. In C, this prevents a header from getting #included twice. This is known as the "internal include guard" (with thanks to Story Teller for pointing that out).
write_hello.h
#ifndef WRITE_HELLO_H
#define WRITE_HELLO_H
#include <stdio.h>
void write_hello(FILE*);
#endif
write_hello.c
#include <stdio.h>
#include "write_hello.h"
void write_hello(FILE* fp){
fprintf(fp,"hello");
printf("Done\n");
}
这篇关于错误:未知类型名称'FILE'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!