错误:未知类型名称'FILE'

Har*_*ave 8 c gcc

我正在创建一个只是向文件写"hello"的函数.我把它放在一个不同的文件中并将其标题包含在程序中.但是gcc给出了一个错误:错误:未知类型名称'FILE'.代码如下

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;
}
Run Code Online (Sandbox Code Playgroud)

write_hello.h:

void write_hello(FILE*);
Run Code Online (Sandbox Code Playgroud)

write_hello.c:

void write_hello(FILE* fp){
    fprintf(fp,"hello");
    printf("Done\n");
}
Run Code Online (Sandbox Code Playgroud)

当通过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){
                  ^
Run Code Online (Sandbox Code Playgroud)

对不起任何错误.我是初学者.

Sco*_*ttK 12

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
Run Code Online (Sandbox Code Playgroud)

write_hello.c

#include <stdio.h>
#include "write_hello.h"
void write_hello(FILE* fp){
    fprintf(fp,"hello");
    printf("Done\n");
}
Run Code Online (Sandbox Code Playgroud)