简体   繁体   English

为什么我的程序在VS2013中调试时无法打开文件?

[英]Why can't my program open a file when debugging in VS2013?

This is pretty bare-bones, meant to just get the program going so I can debug the more complex parts: 这是非常简单的,只是为了让程序运行,所以我可以调试更复杂的部分:

//open file
cout << "Input File Name: ";
string fileName;    
cin >> fileName;
ifstream file;  
file.open(fileName, ios_base::in);
if (!(file.good())){
    cout << "File Open Error\n";        
    return 0;
}

The program compiles fine. 该程序编译良好。 If I execute the debug executable from \\Projects\\[this project]\\Debug\\[program].exe by just double-clicking or browsing there via cmd, it will open the file (which is stored in that same directory) and the rest of the program hums along nicely (until it gets to the buggy parts I actually want to debug, anyways). 如果我通过双击或通过cmd浏览来执行来自\\ Projects \\ [this project] \\ Debug \\ [program] .exe的调试可执行文件,它将打开文件(存储在同一目录中),其余的该程序很好地哼唱(直到它到达我实际想要调试的有缺陷的部分,无论如何)。

However, if I try to 'Start Debugging' from within VS2013, the above fails; 但是,如果我尝试从VS2013中“开始调试”,则上述操作失败; it prints the error and immediately closes. 它打印错误并立即关闭。 The cmd window that the program is executing in when in debugging mode of course shows the directory in the title area, and it is definitely the same directory, but I guess it's looking for the file somewhere else. 程序在调试模式下执行的cmd窗口当然会显示标题区域中的目录,它绝对是同一个目录,但我想它正在寻找其他地方的文件。 I tried copying it to the volume root as well, no joy there. 我尝试将它复制到卷根,没有欢乐。 I am certain this worked just fine in earlier versions of VS, but maybe I'm just brain-farting here. 我确信这在VS的早期版本中运行得很好,但也许我只是在这里放大脑。 Any ideas? 有任何想法吗?

In the project properties in Visual Studio, you can set the current directory in which to start the debugged program. 在Visual Studio的项目属性中,您可以设置启动调试程序的当前目录。 By default, this is set to the location of the .vcxproj file, ie it's not the location of the executable. 默认情况下,它设置为.vcxproj文件的位置,即它不是可执行文件的位置。

At the same time, relative paths passed to std file stream constructors are interpreted relative to the program's current directory, which is why it fails for you when debugging but not when running directly. 同时,传递给std文件流构造函数的相对路径是相对于程序的当前目录解释的,这就是为什么它在调试时失败但在直接运行时失败的原因。 If you instead launched the program using these cmd commands: 如果您使用这些cmd命令启动程序:

>cd some\random\dir
>C:\path\to\your\Projects\[this project]\Debug\[program].exe

It would fail in exactly the same way. 它会以完全相同的方式失败。

To modify the startup directory used by Visual Studio when debugging, go to Project > Properties > Configuration Properties > Debugging > Working Directory . 要在调试时修改Visual Studio使用的启动目录,请转到Project > Properties > Configuration Properties > Debugging > Working Directory Note that the setting is configuration-specific (ie you can have a different startup dir for each configuration). 请注意,该设置是特定于配置的(即,每个配置可以具有不同的启动目录)。 If you want to set it to the directory containing the executable, you can use the macro $(OutDir) . 如果要将其设置为包含可执行文件的目录,可以使用宏$(OutDir)

Perhaps preferably, you might want to move the data file into the project source directory, as it's not a build artifact. 也许最好,您可能希望将数据文件移动到项目源目录中,因为它不是构建工件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM