我现在正在为基本的虚拟文件系统存档(不压缩)编写一个提取器。
我的提取器在将文件写入不存在的目录时遇到问题。
提取函数:
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 上使用 dirname
和 mkdir
。基本上,您应该在调用 ofstream
之前添加一些代码,以确保在必要时通过创建目录来确保目录存在。
关于c++ - 如何使用 ofstream 自动创建目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18682148/