如何自动使用stream创建目录 [英] How to create directories automatically using ofstream

查看:243
本文介绍了如何自动使用stream创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



提取函数:

  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<<补丁< ,<长度< endl;

系统(pause);

delete [] file;
}

ifs vfs根文件, offset 是文件启动时的值, length 是文件长度, path

$



例如path是data / char / actormotion.txt。 >

感谢。

解决方案

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



您可以使用 dirname code> mkdir 在Posix系统上,或Windows等效项或Boost.Filesystem。基本上,您应该在调用 ofstream 之前添加一些代码,以确保该目录存在,如果必要的话创建它。


I am now writing an extractor for a basic virtual file system archive (without compression).

My extractor is running into problems when it writes a file to a directory that does not exist.

Extract function :

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 is the vfs root file, offset is the value when the file starts, length is the file length and path is a value from file what save offsets len etc.

For example path is data/char/actormotion.txt.

Thanks.

解决方案

ofstream never creates directories. In fact, C++ doesn't provide a standard way to create a directory.

Your could use dirname and mkdir on Posix systems, or the Windows equivalents, or Boost.Filesystem. Basically, you should add some code just before the call to ofstream, to ensure that the directory exists by creating it if necessary.

这篇关于如何自动使用stream创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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