如何在包含的 c++ 中写入外部文件的路径来源
假设我正在编写一个库或一组工具 mytool
,其中定义了其他人可以使用的类 MyTool
。假设我有一个像这样的目录树:
project
| - program1
| - main1.cpp
...
| - mytool
| - mytool.h
| - mytool.cpp
| - data.txt
在 tool1.cpp
中,我使用外部二进制大文件 data.dat
:
ifsteam f("data.txt");
main1.cpp
使用mytool,但如果 mytool.(s)o
与 main1.o
链接,则程序无法找到 data.dat
,在这种情况下我需要将上一行更改为:
ifstream f("../mytool/data.txt");
但我不知道其他人将 mytool
放在哪里,例如他们可以有不同的目录树:
project
| - program1
| - main1.cpp
| - mytool
| - tool1.h
| - tool2.cpp
| - data.dat
另外(我是对的吗?)路径取决于位置程序被执行。
我能想到的唯一解决方案是将 data.dat
的路径传递给类构造函数 MyTool
,但我想为用户隐藏此文件。
Suppose I'm writing a library or a set of tools mytool
where a class MyTool
that other people can use is defined. Suppose I have a directory tree like this:
project
| - program1
| - main1.cpp
...
| - mytool
| - mytool.h
| - mytool.cpp
| - data.txt
in tool1.cpp
I use the external binary huge file data.dat
:
ifsteam f("data.txt");
the main1.cpp
use mytool, but if mytool.(s)o
is linked with main1.o
the program can't find data.dat
, for this case I need to change the previous line to:
ifstream f("../mytool/data.txt");
but I can't know where other people put mytool
for example they can have a different directory tree:
project
| - program1
| - main1.cpp
| - mytool
| - tool1.h
| - tool2.cpp
| - data.dat
In addition (am I right?) the path depend on where the program is executed.
The only solution I can imagine is to pass to the class contructor MyTool
the path of data.dat
but I want to keep hidden this file for the user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要知道文件的绝对路径,或者文件相对于工作目录的路径。一种方法是让用户在编译程序之前运行一个配置脚本。然后,该脚本将相关路径硬编码到您的程序中,以便程序以为用户定制的方式硬连线路径。
有时这不是一个选项,因为您不想分发源代码,或者因为您希望允许路径在运行时更改。然后您可以在运行时读取配置文件,其中说明了该文件的位置。但这只是一个抽象层:您仍然需要知道配置文件在哪里。例如,您可以询问系统用户的个人目录在哪里,然后在该目录中找到该文件。这是路径的编译时和运行时计算之间的一种混合。
You need to know the absolute path of the file, or else the path of the file relative to your working directory. One approach is to have a configuration script which the user runs before compiling your program. The script then hardcodes into your program the relevant path, so the program has the path hardwired in a manner customized for the user.
Sometimes that's not an option because you don't want to distribute the source code, or because you wish to allow the path to change at runtime. Then you can read a configuration file at runtime which says where the file is. But this is just a layer of abstraction: you still need to know where that configuration file is. You might, for example, ask the system where the user's personal directory is, and then find the file there at that directory. This is a sort of mix between compile-time and runtime computation of the path.
一种选择是使用环境变量来指定工具的位置。例如,将其命名为 MYTOOLDIR。您可以设置MyTool的安装路径。调用
getenv("MYTOOLDIR");
可以解析路径。在 Windows 上,在 mytool 目录中运行
SETX PATH=%PATH%;./
,或者在 Linux 上,只需PATH=$PATH:./
。 (提供一个 set_env.bat 或其他类似的东西来执行此操作。)One option would be to use an environment variable for the location of your tools. For instance, name it MYTOOLDIR. You can set the path on installation of MyTool. A call to
getenv("MYTOOLDIR");
can resolve the path.On windows, within the mytool dir, run
SETX PATH=%PATH%;./
, or on Linux, justPATH=$PATH:./
. (Provide a set_env.bat or whatnot to do it.)您需要将二进制文件的位置设置为用户在程序的特定安装上定义的配置值。或者,更简单的是,始终将二进制文件放在与最终可执行文件相同的位置,并使用“data.dat”作为路径。
You'll need to make the location of the binary file a configuration value that the user defines on a particular installation of the program. Or, more easily, just always put the binary file in the same place as the final executable and use "data.dat" as the path.