1

您好,我正在尝试在 Microsoft Visual Studio Ultimate 2010 中执行由以下代码生成的 .exe 文件,但没有看到正在创建的文件。

这段代码在使用 GCC 的 Linux 中编译和执行时工作得非常好。

重复一遍,我可以使用在 Linux 中创建的文件!但在 Windows 中,.exe 程序无法为用户在命令提示符下输入的名称创建文件。

有人可以让我知道我在编译器方面出了什么问题吗?

真诚的感谢

// filename.cpp : Defines the entry point for the console application.

    #include "stdafx.h"        //Please comment if code is to be executed in GCC
    #include <stdio.h>
    #include <stdlib.h>
    #include <memory.h>

    int main()
    {
        FILE *pFile;
        char cNotes_buffer[100];
        memset(cNotes_buffer,0,100);
        printf("Please enter name of the LOG file - with tag MAX 100 characters!! \n");
        if( fgets (cNotes_buffer , 100 , stdin) != NULL )
            {
            fclose (stdin);
        }

        printf("name of file %s \n", cNotes_buffer);
        if ( ( pFile = fopen(cNotes_buffer,"a+") ) == NULL )
        {
            printf("ERROR OPENING FILE FOR LOGGING \n");
            exit(0);
        }
        return 0;
    }
4

2 回答 2

3

ENTER输入文件名后,您很可能会按。所以\n也进入cNotes_buffer。现在您尝试创建一个以该缓冲区为文件名的文件。而且我认为您不能\n在其中创建文件名。

如果您按下了返回键,请执行类似操作

cNotes_buffer[strlen(cNotes_buffer)-1]=0;

编辑:换行符可以出现在 linux 中的文件名中,但在Windows上则不然这解释了为什么您的文件是在 Linux 中创建的,而不是在 Windows 上创建的。

于 2012-04-18T11:19:26.243 回答
0

尝试fclose(pFile)。未能关闭文件会导致奇怪的行为。

于 2012-04-18T11:15:35.707 回答