只有在Visual Studio外部运行时才会找到文本文件 [英] Text file only found when running outside of Visual Studio

查看:193
本文介绍了只有在Visual Studio外部运行时才会找到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目(本机C ++编译为.exe)在VS 2010之外运行良好,但在VS内部(有或没有调试器),它被卡住试图找到一个文本文件位于同一bin文件夹。

I've got an a project (native C++ compiled to .exe) that runs fine outside of VS 2010, but inside of VS (with or without the debugger) it gets stuck trying to find a text file located in the same bin folder as it.

有什么想法为什么会发生?我的预期是,VS试图打开一个文件时,代码看起来在哪里,但我不知道足够的细节,以纠正这一点。

Any ideas as to why this would happen? My hunch is that VS messes with where the code looks when trying to open a file, but I don't know enough details to correct this.

一些细节:

Some details:

我的.exe从我之前写的.dll文件中调用了一个函数,后者又试图找到一个指定该函数参数的文本文件。为方便起见,我将所有这些文件放在同一个文件夹中,因此查找文本文件不会有问题。

My .exe calls a function from a .dll I wrote earlier, which in turn tries to find a text file specifying that function's parameters. For convenience I've placed all of these files in the same folder, so finding the text file wouldn't be a problem.

打开文件之前,函数检查它存在使用:

Before opening the file, the function checks that it exists using:

PersistentAssert(Utility::FileExists(Filename), "Parameter file not found");

在VS 2010中,此行导致显示未找到参数文件,但在VS外部

In VS 2010 this line causes "Parameter file not found" to display, but outside of VS the program manages to find the file.

推荐答案

默认情况下,从visual studio启动可执行文件时,它使用项目文件目录作为当前工作目录。您可以在配置属性 - >调试 - >工作目录中更改它。

When launching an executable from visual studio by default it uses the project file directory as the current working directory. You can change it in Configuration Properties -> Debugging -> Working Directory.

但是我不认为从当前工作目录搜索程序数据文件等是正确的行为。相反,你的程序应该找到可执行文件的目录,并找到该目录中的文件。

However I don't think it's correct behavior to search your programs datafiles etc from the current working directory. Instead your program should find out the directory of the executable and find the files in that directory.

这篇关于只有在Visual Studio外部运行时才会找到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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