这个
C++17 filesystem
基于boost.filesystem。
#include <filesystem>
namespace fs = std::experimental::filesystem;
我遍历一个目录
for (auto& p: fs::directory_iterator("media"))
我想将路径传递给一个函数,该函数将filepath作为
const char *
我发现了一个类似的问题
boost filesystem here
.
path in C++17
是基于
value_type
.
值类型
:文件系统本机编码使用的字符类型:POSIX上的字符,Windows上的wchar\u t
const wchar_t *
一串
以下“工作”对我来说:
char file[2000];
wcstombs(file, p.path().c_str(), 2000);
auto image = SDL_LoadBMP(file);
我正在寻找一个不同的版本,因为这个实现有各种各样的混乱(数组到指针的衰减和_CRT_SECURE_NO_警告)。
我正在寻找一个更漂亮的版本,可以直接从路径到路径
常量字符*
在windows上使用新的C++17文件系统。
#define _CRT_SECURE_NO_WARNINGS 1
#include <SDL.h>
#include <vector>
#include <filesystem>
namespace fs = std::experimental::filesystem;
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
auto window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 400, SDL_WINDOW_SHOWN);
auto scrrenSurface = SDL_GetWindowSurface(window);
auto images = std::vector<SDL_Surface*>();
for (auto& p: fs::directory_iterator("media"))
{
char file[2000];
wcstombs(file, p.path().c_str(), 2000);
auto image = SDL_LoadBMP(file);
images.push_back(image);
}
for (auto&image : images)
{
SDL_BlitSurface(image, NULL, scrrenSurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(2000);
}
for (auto&image : images)
SDL_FreeSurface(image);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
编辑:
在评论部分有一个类似的问题
so-question
这些问题是同一核心问题的不同表现形式,即将路径转换为可由另一种方法使用的格式。我反对删除这个问题,因为这两个问题都是通过不同的搜索找到的。