您好,我正试图执行由以下代码在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;
    }

最佳答案

输入文件名后,您很可能会按回车键。所以\n也会进入cNotes_buffer。现在尝试创建一个文件,并将此缓冲区作为文件名。我不认为你可以用\n创建文件名。
做一些类似于按下回车键的事情

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

编辑:新行可以出现在Linux中的filenames中,但不能出现在Windows中,这解释了为什么您的文件是在Linux中创建的,而不是在Windows中创建的。

关于c - 通过fopen创建文件在Linux上运行良好,但在Windows上却无法运行(MS VS 2010),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10208072/

10-11 22:15
查看更多