C ++文件路径问题

时间:2015-07-12 19:50:48

标签: c++ directory sdl filepath

当我使用此代码加载SDL渲染图像时:

SDL_Texture* testimg = IMG_LoadTexture(renderer, "testz.bmp");

(或)

SDL_Texture* testimg = IMG_LoadTexture(renderer, "/testz.bmp");

图像根本无法渲染。但是......如果我使用这段代码:

SDL_Texture* testimg = IMG_LoadTexture(renderer, "../bin/testz.bmp");

SDL绘制图像就好了。 "箱"是.exe所在的文件夹。那么如何修复此文件路径问题?

编辑:另一种可能是视觉工作室,由于某种原因,正在运行它放在bin文件夹中的exe文件在另一个没有图像的位置......

2 个答案:

答案 0 :(得分:0)

因为安装可执行文件的目录与操作系统启动的进程的当前目录不同,所以当它运行此可执行文件时。

很明显,启动进程的当前目录是其可执行文件的兄弟目录。

答案 1 :(得分:0)

可能有用的一件事是使用function retrieveTutorials(){ return $.ajax({ type: "GET", url: "/tutorials/retrieve", dataType: "json" }); } $('#main-content-wrap').on('click', '.recentTutorials', function(){ //store our function call as an ajax promise var promise = retrieveTutorials(); //wait for the ajax to return so we can mutate the data promise.done(function(data){ //now our data will be properly recentFirstPage = data.next_page_url; }); }); 来确保使用正确的基本路径。假设您正在使用标准argv[0],它将是这样的:

main()

然后你的装载会是这样的:

std::string base_path;
int main(int argc, char* argv[]) {
    base_path = argv[0];
    size_t last_slash = base_path.find_last_of('/');
    if(last_slash == std::string::npos)
        base_path = ".";
    else base_path.erase(last_slash + 1);
    // Anything else you need to do in `main()`
    return 0;
}

如果文件与SDL_Texture* testimg = IMG_LoadTexture(renderer, base_path + "testz.bmp"); 不同,则您还需要使用main()重新声明base_path

extern

当然,您需要extern std::string base_path; 才能实现此目的。

如果您在#include <string>内完成所有加载,则可以通过将main()的声明移至base_path来提高效率:

main()