C语言 错误:未知类型名称“文件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42744280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: unknown type name ‘FILE’
提问by Harsh Dave
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
我正在制作一个函数,它只是将“hello”写入文件。我已经把它放在一个不同的文件中,并在程序中包含了它的头文件。但是 gcc 给出了一个错误,即:错误:未知类型名称“文件”。代码如下
app.c:
应用程序.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:
write_hello.h:
void write_hello(FILE*);
write_hello.c:
write_hello.c:
void write_hello(FILE* fp){
fprintf(fp,"hello");
printf("Done\n");
}
when compiling by gcc the following occurs:
通过 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){
^
Sorry for any mistakes.I am a beginner.
抱歉有任何错误。我是初学者。
回答by ScottK
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).
FILE 在 stdio.h 中定义,您需要将其包含在每个使用它的文件中。所以 write_hello.h 和 write_hello.c 都应该包含它,而 write_hello.c 也应该包含 write_hello.h(因为它实现了 write_hello.h 中定义的函数)。
Also note that it is standard practice for every header file is 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). All system headers such as stdio.h include an internal include guard. All user defined headers should also include an internal include guard as shown in the example below.
另请注意,每个头文件的标准做法是定义一个同名的宏 (IN CAPS),并将整个头包含在 #ifndef、#endif 之间。在 C 中,这可以防止标题被 #included 两次。这被称为“内部包含守卫”(感谢 Story Teller 指出这一点)。所有系统头文件,如 stdio.h 都包含一个内部包含保护。所有用户定义的标头还应该包括一个内部包含保护,如下面的示例所示。
write_hello.h
write_hello.h
#ifndef WRITE_HELLO_H
#define WRITE_HELLO_H
#include <stdio.h>
void write_hello(FILE*);
#endif
write_hello.c
write_hello.c
#include <stdio.h>
#include "write_hello.h"
void write_hello(FILE* fp){
fprintf(fp,"hello");
printf("Done\n");
}
Note that when you include system files, the header name is placed within <>'s. This helps the compiler identify that these headers are system headers which are stored in a central place based on your development environment.
请注意,当您包含系统文件时,标头名称放在<>'s 中。这有助于编译器识别这些头文件是系统头文件,它们根据您的开发环境存储在中央位置。
Your own custom headers are placed in quotes ""are typically found in your current directory, but may be placed elsewhere by including the path, or adding the directory to your list of directories that the compiler searches. This is configurable within your project if you are using an IDE like NetBeans, or using the -I compiler option is building it directly or through a makefile.
您自己的自定义标头放在引号""中,通常可以在当前目录中找到,但也可以通过包含路径或将目录添加到编译器搜索的目录列表中来放置在其他地方。如果您使用像 NetBeans 这样的 IDE,或者使用 -I 编译器选项直接或通过 makefile 构建它,那么这可以在您的项目中进行配置。

