无法打开文件以输入C ++ [英] Can't open file for input C++

查看:54
本文介绍了无法打开文件以输入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函数(仅输出错误消息并退出程序).

In this section of my code, I take in a string for the name of the input file to be opened and if nothing is entered, I set it to DEF_IN_NAME (Which is "input.txt"). I have tried entering "i.txt" as well as entering nothing (defaulting to "input.txt") and in both cases my program calls the die function (simply outputs error message and exits program).

我的文件与.exe文件位于同一文件夹中,所以我不确定为什么会这样.是什么原因造成的,我该如何解决?

My files are in the same folder as my .exe file so I'm not sure why this is happening. What is causing this and how do I fix it?

我应该提到第二次死亡的呼叫是执行的,而不是第一次.也就是说,无法打开文件进行输入.

I should mention that the second call to die is performed, not the first. That is, the file cannot be opened for input.

我正在Windows 10上从Visual Studio Express 2013(调试)运行它

I am running this on windows 10 from Visual Studio Express 2013 (debugging)

推荐答案

如果未指定要读取的文件的显式路径,则运行时库将默认为进程当前的工作目录.如果在调试器中运行程序,则当前的工作目录将是您的项目目录,而不是可执行文件的链接位置.

If you do not specify the explicit path for the file to be read, the run time library will default to the process current working directory. If you are running the program in the debugger, the current working directory will be your project directory rather than the location where the executable is linked.

这篇关于无法打开文件以输入C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆