如何在不设置完整路径的情况下访问 C++ 中的资源 [英] How can i access resources in c++ without setting the full path

查看:41
本文介绍了如何在不设置完整路径的情况下访问 C++ 中的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在程序中访问我的资源,但我不想使用包含 C:\Users\USER_EXAMPLE\...的完整路径在 java 中有一个选项可以使用 getClass.getResources("Folder/test.txt");我想做的是,读取和写入文件.它工作正常.但我只是想知道如果您在另一台电脑上执行该程序将无法正常工作.因为它没有检测到文件.

<预><代码>string path = "C:\\Users\\USER_EXAMPLE\\source\\repos\\Console\\Console\\Data.txt";inFile.open("C:\\Users\\USER_EXAMPLE\\source\\repos\\Console\\Console\\Data.txt");inFileWrite.open(path, ios_base::app);```

解决方案

无论如何,如果你想访问一些资源,你必须知道路径.

如果您不想使用绝对路径(这是我出于可移植性原因而理解的),我认为您可以使用相对路径.

更清楚地说,简单地使用相对路径是不好的,因为正如一些程序员埃里克的回答中评论的那样,相对路径是相对于工作目录的.因此,如果您从其位置目录以外的其他目录启动可执行文件,则相对路径将被破坏.

但是有一个解决方案:
如果使用main()参数,可以获得可执行位置的绝对路径.
实际上,argv[0] 包含被调用的命令,即:absolute_path/executable_name.您只需删除可执行文件名称,即可获得可执行文件夹的绝对路径.
您现在可以使用相对于该路径的路径.

它可能如下所示:

#include int main(int argc, char ** argv){//获取命令const std::string called_cmd = argv[0];//找到可执行文件名size_t n = called_cmd.rfind("\\");//窗户//size_t n = called_cmd.rfind("/");//Linux, ...//处理潜在的错误(应该永远不会发生,但它是为了健壮性目的)if(n == std::string::npos)//如果没有找到模式返回-1;//删除可执行文件名std::string executable_folder = called_cmd.substr(0, n);//...返回0;}

我认为它会帮助你.

<小时>

事实上,正如已经提到的,argv[0] 包含被调用的命令.所以严格来说,它不一定是可执行文件的绝对路径".
实际上,如果从具有相对路径的控制台/终端调用可执行文件,这就是 argv[0] 将得到的.
但无论如何,路径解析问题在调用时解决,因此,如果我们使用相对于给定 argv[0] 路径的路径,它总是有效的.

I wish to access my Resources in a programm but i dont want to use the full path which includes C:\Users\USER_EXAMPLE\... In java there is an option to use getClass.getResources("Folder/test.txt"); The thing i want to do is, read and write a file. And it works fine. But im just wondering if you execute the program on a different pc it will not work. Because it dosnt detect the file.


    string path = "C:\\Users\\USER_EXAMPLE\\source\\repos\\Console\\Console\\Data.txt";
    inFile.open("C:\\Users\\USER_EXAMPLE\\source\\repos\\Console\\Console\\Data.txt");
    inFileWrite.open(path, ios_base::app);```

解决方案

In any case, if you want to access some resources, you will have to know the path.

If you don't want to use absolute paths (which is something I understand for portability reasons), I think you can use kind of relative paths.

To be more clear, simply using relative paths is bad because, as Some programmer dude commented in Eric's answer, the relative path is relative to the working directory. Consequently, if you launch the executable from another directory than its location directory, then the relative paths will be broken.

But there is a solution:
If you use the main() parameters, you can get the absolute path of the executable location.
In fact, argv[0] contains the called command which is: absolute_path/executable_name. You just have to remove the executable name and you get the absolute path of the executable folder.
You can now use paths relative to this one.

It may look as follows:

#include <string>

int main(int argc, char ** argv)
{
    // Get the command
    const std::string called_cmd = argv[0];

    // Locate the executable name
    size_t n = called_cmd.rfind("\\"); // Windows
    //size_t n = called_cmd.rfind("/"); // Linux, ...

    // Handle potential errors (should never happen but it is for robustness purposes)
    if(n == std::string::npos) // if pattern not found
        return -1;

    // Remove the executable name
    std::string executable_folder = called_cmd.substr(0, n);

    // ...

    return 0;
}

I think it will help you.


EDIT:

In fact, as already mentioned, argv[0] contains the called command. So to be rigorous, it is not necessarily the "absolute path" to the executable.
Indeed, if the executable was called from a console/terminal with a relative path, this is what argv[0] will get.
But in any case, the path resolution problem is resolved at call time, consequently it will always work if we use paths relative to the given argv[0] path.

这篇关于如何在不设置完整路径的情况下访问 C++ 中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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