无法打开输入 C++ 的文件
Can't open file for input C++
string iName, oName; //input and output file names, respectively
double avgIn;
cout << "Enter file name for input: ";
getline(cin, iName) || die("Could not read input file name");
if (iName.empty())
iName = DEF_IN_NAME;
ifstream fin;
fin.open(iName);
if (!fin) die("Could not open " + iName + " for input\n");
在我的这部分代码中,我输入了一个字符串作为要打开的输入文件的名称,如果没有输入任何内容,我将其设置为 DEF_IN_NAME(即 "input.txt" ).我试过输入 "i.txt" 以及不输入任何内容(默认为 "input.txt"),在这两种情况下我的程序都调用 die 函数(只是输出错误消息并退出程序)。
我的文件与我的 .exe 文件位于同一文件夹中,所以我不确定为什么会这样。是什么原因造成的,我该如何解决?
编辑:我应该提到执行了第二次死亡调用,而不是第一次调用。即无法打开文件进行输入。
我是 运行 这个 windows 10 来自 Visual Studio Express 2013(调试)
如果不指定读取文件的显式路径,运行时间库默认为进程当前工作目录。如果您 运行 在调试器中安装程序,当前工作目录将是您的项目目录,而不是可执行文件链接的位置。
string iName, oName; //input and output file names, respectively
double avgIn;
cout << "Enter file name for input: ";
getline(cin, iName) || die("Could not read input file name");
if (iName.empty())
iName = DEF_IN_NAME;
ifstream fin;
fin.open(iName);
if (!fin) die("Could not open " + iName + " for input\n");
在我的这部分代码中,我输入了一个字符串作为要打开的输入文件的名称,如果没有输入任何内容,我将其设置为 DEF_IN_NAME(即 "input.txt" ).我试过输入 "i.txt" 以及不输入任何内容(默认为 "input.txt"),在这两种情况下我的程序都调用 die 函数(只是输出错误消息并退出程序)。
我的文件与我的 .exe 文件位于同一文件夹中,所以我不确定为什么会这样。是什么原因造成的,我该如何解决?
编辑:我应该提到执行了第二次死亡调用,而不是第一次调用。即无法打开文件进行输入。
我是 运行 这个 windows 10 来自 Visual Studio Express 2013(调试)
如果不指定读取文件的显式路径,运行时间库默认为进程当前工作目录。如果您 运行 在调试器中安装程序,当前工作目录将是您的项目目录,而不是可执行文件链接的位置。