我正在创建一个函数,它只是将“hello”写入文件。 我把它放在一个不同的文件中并将其标题包含在程序中。但是gcc给出了一个错误:错误:未知类型名称’FILE’。 代码如下
app.c:
#include #include #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("Donen"); }
当通过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 void write_hello(FILE*); #endif
write_hello.c
#include #include "write_hello.h" void write_hello(FILE* fp){ fprintf(fp,"hello"); printf("Donen"); }
以上就是c/c++开发分享错误:未知类型名称’FILE’相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/545578.html