由于某些奇怪的原因,文件声明在Visual Studio中的工作方式与在代码块中的工作方式不同。 下面的代码在代码中运行得非常好:块但是不能在Visual Studio中运行,而且我不知道为什么不能解决它。
int n;
FILE * pFile;
pFile = fopen ("date.in","r");
fscanf(pFile,"%d",&n);
printf("%d", n);
return 0;
分别使用cstdio和stdafx 编辑1:
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int d;
int main ()
{
FILE * inFile;
inFile = fopen("date.in", "r");
fscanf(inFile, "%d", &d);
printf("%d", d);
return 0;
}
触发stdio.h的断点1061行
答案 0 :(得分:2)
问题是您没有检查fopen
的结果,但它已失败。这几乎可以肯定,因为程序运行时data.in
不在当前目录中(但是是其他目录)
您可以在项目属性中指定运行程序时的当前目录。或者,在命令行上传递数据文件的完整路径,并在项目属性中指定程序的参数。