gpt4 book ai didi

c - 通过 fopen 创建文件在 Linux 上运行良好但在 Windows 上运行不正常(MS VS 2010)

转载 作者:可可西里 更新时间:2023-11-01 11:36:29 24 4
gpt4 key购买 nike

您好,我正在尝试在 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;
}

最佳答案

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

如果你按下了回车键,做一些类似的事情

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

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com