C++ 打开当前目录下的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19761069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening a file in the current directory
提问by user2827019
I'm trying to load files, and previously I was using hardcoded file locations, (like "c:\location\file.txt") but now that a few friends are also using the file, I'd like to allow them to put the executable wherever they want.
我正在尝试加载文件,以前我使用硬编码的文件位置(如“c:\location\file.txt”)但现在有几个朋友也在使用该文件,我想允许他们将可执行文件放在他们想要的任何地方。
my current code looks like:
我当前的代码看起来像:
ifstream myfile;
myfile.open("c:\client\settings.cfg");
I'm trying to change it so that the user puts their executable into whatever folder they want, and then they create a folder and put their settings file into it and the exe will load that with their settings.
我正在尝试更改它,以便用户将他们的可执行文件放入他们想要的任何文件夹中,然后他们创建一个文件夹并将他们的设置文件放入其中,exe 将加载他们的设置。
ifstream myfile;
myfile.open("\settings\settings.cfg");
I have some basic error handling in place, and now the program always errors out saying that it can't find the file.
我有一些基本的错误处理,现在程序总是出错,说它找不到文件。
The file structure looks like this:
文件结构如下所示:
[ART]
asset.png
[SETTINGS]
settings.cfg
client.exe
This seems like a really simple thing to do, but I can't find any way to do it. Every example and tutorial about reading and writing to files deals only with files in the executable's directory, or hardcoded into c:\folder...
这似乎是一件非常简单的事情,但我找不到任何方法来做到这一点。每个关于读取和写入文件的示例和教程仅处理可执行文件目录中的文件,或硬编码到 c:\folder...
Could anyone point me to how I do this?
谁能指出我如何做到这一点?
回答by Zac Howland
The search path for most systems starts with the current working directory and then to a PATH
environment variable. So, all you need to do is specify the file/folder without the absolute path markings and it will use the path relative to the working directory:
大多数系统的搜索路径从当前工作目录开始,然后是PATH
环境变量。因此,您需要做的就是指定没有绝对路径标记的文件/文件夹,它将使用相对于工作目录的路径:
ifstream myfile;
myfile.open("settings\settings.cfg");
// ^^ Note the lack of \ to start the file path
回答by Nikos C.
Paths beginning with \
are always relative to the current drive's root directory. If the current drive is C:
, then \settings\settings.cfg
means C:\settings\settings.cfg
.
以 开头的路径\
总是相对于当前驱动器的根目录。如果当前驱动器是C:
,则\settings\settings.cfg
表示C:\settings\settings.cfg
。
Note that you can use /
in order to avoid escaping everything. So you can use: settings/settings.cfg
. This will be relative to the user's current directory. Note however, that this doesn't necessarily correspond to the directory where the executable resides. If you need the directory of the executable, then you need to use a Windows API function to get it:
请注意,您可以使用/
以避免转义所有内容。所以你可以使用:settings/settings.cfg
。这将相对于用户的当前目录。但是请注意,这不一定对应于可执行文件所在的目录。如果您需要可执行文件的目录,那么您需要使用 Windows API 函数来获取它:
#include <Windows.h>
// ...
HMODULE module = GetModuleHandleW(NULL);
WCHAR path[MAX_PATH];
GetModuleFileNameW(module, path, MAX_PATH);
Now if you want to open settings/settings.cfg
relative to the directory of the executable, create a path that starts with path
and append /settings/settings.cfg
to it.
现在,如果你想开settings/settings.cfg
相对于可执行文件的目录下,创建一个路径开始与path
和追加/settings/settings.cfg
到它。