您好,我正嘗試執行由Microsoft Visual Studio Ultimate 2010中的以下代碼生成的.exe文件,但是我沒有看到該文件被創建。通過fopen創建文件可以在Linux上正常工作,但不能在Windows上運行(MS VS 2010)
這段代碼在Linux下用GCC編譯和執行時工作得很好。
重複我能夠使用在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;
}
嘗試使用'perror()'(而不是'printf()')來獲取錯誤消息。用'perror()'你也可以得到更多關於錯誤的信息。 – pmg 2012-04-18 11:25:48
@pmg AFAIK窗口沒有'perror()'。它是'GetLastError()'。 – RedX 2012-04-18 11:31:25
@RedX:除非正在使用的Windows編譯器不符合ANSI C89/ISO C90,否則Windows具有'perror()'。 – pmg 2012-04-18 11:40:20