29

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.

jmstoker
  • 2,874
  • 18
  • 35
Kacper Fałat
  • 593
  • 3
  • 7
  • 16
  • it's not possible with an `std::ofstream`, it's for writing files only. There's probably a good wrapper for creating directories on an arbitrary platform in `boost`. – stefan Sep 08 '13 at 09:18
  • @WhozCraig it will be hard because this archive had 20 000 files in different dirs. – Kacper Fałat Sep 08 '13 at 09:18

3 Answers3

33

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.

Steve Jessop
  • 257,525
  • 32
  • 431
  • 672
  • 10
    +1 the note about C++ not having a standard way of making filesystem directories often comes as a shock to people. Nice to mention it. – WhozCraig Sep 08 '13 at 09:24
  • thanks for this. i had no idea..battled with my code for hours trying to understand what the hell happened. i could write files for days and it suddenly stopped. Didnt realize there was another piece of code which took care of folder creation.. – Alexandru Lucian Susma Mar 13 '15 at 01:20
  • 9
    [A standard way to create directories](http://en.cppreference.com/w/cpp/filesystem/create_directory) has been added along with `std::filesystem` library in C++ 17. Currently [no compiler has supported it fully and officially](http://en.cppreference.com/w/cpp/compiler_support#C.2B.2B17_features) though. – ebk Feb 15 '18 at 07:33
21

Its not possible with ofstream to check for existence of a directory

Can use boost::filesystem::exists instead

    #include <boost/filesystem.hpp>

    boost::filesystem::path dir("path");

    if(!(boost::filesystem::exists(dir))){
        std::cout<<"Doesn't Exists"<<std::endl;

        if (boost::filesystem::create_directory(dir))
            std::cout << "....Successfully Created !" << std::endl;
    }
Community
  • 1
  • 1
P0W
  • 42,046
  • 8
  • 62
  • 107
7

Creating a directory with ofstream is not possible. It is mainly used for files. There are two solutions below:

Solution 1:

#include <windows.h>
int _tmain() {
    //Make the directory
    system("mkdir sample");
}

Solution 2:

#include <windows.h>
int _tmain() {
    CreateDirectory("MyDir", NULL);
}