Code ::使用MinGW编译器的块 - fprintf导致空文件

时间:2013-09-27 15:22:09

标签: c windows mingw codeblocks

到目前为止,我只使用Linux进行编码(基本上是gcc和Kate的命令行)。我想进入Windows编程,因为我有权访问运行Win7的所有功能更强大的机器。

所以我下载了代码块和minGW。 Hello world运行正常,但是当涉及到打印文件时,它只是给我一个空文件。我是否犯了新手的错误?它创建了testfile.txt,但执行后该文件为空。

代码:

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv)
{
    FILE *test;
    if (test=fopen("testfile.txt","w")==NULL)
    {
            printf("Open Failed\n");
            abort();
    }
    int i=9;
    fprintf(test,"This is a test %d\n",i);
    fclose(test);
    return 0;
}

1 个答案:

答案 0 :(得分:2)

这是不正确的:

if (test=fopen("testfile.txt","w")==NULL)

你的if语句应该是:

 if ((test=fopen("testfile.txt","w"))==NULL)

因此文件“testfile.txt”将正确打开,您将获得预期的输出:

This is a test 9 
相关问题