gpt4 book ai didi

c++ - 如何使用 ofstream 自动创建目录

转载 作者:IT老高 更新时间:2023-10-28 12:55:54 29 4
gpt4 key购买 nike

我现在正在为基本的虚拟文件系统存档(不压缩)编写一个提取器。

我的提取器在将文件写入不存在的目录时遇到问题。

提取函数:

void extract(ifstream * ifs, unsigned int offset, unsigned int length, std::string path)
{
char * file = new char[length];

ifs->seekg(offset);
ifs->read(file, length);

ofstream ofs(path.c_str(), ios::out|ios::binary);

ofs.write(file, length);
ofs.close();

cout << patch << ", " << length << endl;

system("pause");

delete [] file;
}

ifs是vfs根文件,offset是文件开始时的值,length是文件长度,path 是文件中保存偏移量 len 等的值。

例如路径是 data/char/actormotion.txt。

谢谢。

最佳答案

ofstream 从不创建目录。事实上,C++ 并没有提供创建目录的标准方法。

您可以在 Posix 系统或 Windows 等效系统或 Boost.Filesystem 上使用 dirnamemkdir。基本上,您应该在调用 ofstream 之前添加一些代码,以确保在必要时通过创建目录来确保目录存在。

关于c++ - 如何使用 ofstream 自动创建目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18682148/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com