简体   繁体   English

C - “文件”错误的冲突类型

[英]C - Conflicting types for 'file' error

When I try to compile my C program, I get the following error: 当我尝试编译我的C程序时,我收到以下错误:

file.c:16:1: error: conflicting types for 'file'

concerning this code: 关于这段代码:

char *filename = "dictionary.txt";
FILE *file;
file = fopen(filename, "a+");

I have used the article as a reference: C File I/O and Binary File I/O 我使用该文章作为参考: C文件I / O和二进制文件I / O.

Any help would be much appreciated, and thanks in advance. 任何帮助将不胜感激,并提前感谢。

Edit: here is the full code http://pastebin.com/3iMmGzfk 编辑:这是完整的代码http://pastebin.com/3iMmGzfk

With this code I get the same error: conflicting types for 'file' . 使用此代码,我得到了相同的error: conflicting types for 'file' Note the definition of file twice in the same scope. 请注意在同一范围内两次定义文件。

#include <stdio.h>

int main(void) {
    int file = 0;

    char *filename = "dictionary.txt";
    FILE *file;
    file = fopen(filename, "a+");

    return 0;
}

Making sure there is not another variable named file in the same scope should solve the problem. 确保在同一范围内没有另一个名为file的变量可以解决问题。

For example I don't get a compiler error with this version: 例如,我没有使用此版本的编译器错误:

#include <stdio.h>

int main(void) {
    int file = 0;

    {
        char *filename = "dictionary.txt";
        FILE *file;
        file = fopen(filename, "a+");
    }
    return 0;
}

The article you link to actually uses fp for the FILE * variable name, but if your own code doesn't elsewhere define a file , then somewhere a header file you are directly or indirectly using is defining something called file . 您链接到的文章实际上使用fp作为FILE *变量名称,但如果您自己的代码没有在其他地方定义file ,那么您直接或间接使用的头文件定义了一个名为file东西。 Without knowing your platform, it's hard to say which header file would be defining a file . 在不了解您的平台的情况下,很难说哪个头文件会定义file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM