Question

Pseudo code:

for(int i=0;i<m_iNumOfClass;i++) {

    char str[200];

    if(iterk doesn't exist)
        sprintf(str, "iterk\\HMMtransiMean%d.txt", i);

    iter(iterk exist)
    mkdir(iterk+1)
    sprintf(str, "iterk+1\\HMMtransiMean%d.txt", i);
}

This is pseudo code what i want to do.

I want to create a folder named iterk1 if it doesn't exist. But if it exist, I create a folder named iterk2. and then, create a txt file named HMMtransiMean%d in that folder created just now.

How can i do it? please help me.

Was it helpful?

Solution 2

Note: This solution does not require boost...

I assume you're using windows platform (because you use "\\"):

The first useful function to consider is _mkdir (doc).

You can use the non-zero value to determine whether the folder was created.

To create a file you can use fopen (doc)

This works for me on Windows but should work on Linux as well(with a minor include change from direct.h to #include <sys/stat.h> and #include <sys/types.h> to have mkdir):

#include <iostream>
#include <sstream>
#include <direct.h>
#include <cstdio>
#include <string>
using namespace std;

int main() {
    const string pref = "iterk";
    string path = pref;
    stringstream suffix;
    int i=0;
    int res = -1;
    do{     
        res = mkdir(path.c_str());
        if( res == 0){
            path = path + "/HMMtransiMean" + suffix.str() + ".txt";
            break;
        }
        else{
            ++i;
            suffix.str(string());
            suffix << i;
            path = pref + suffix.str();
        }
    } while (EEXIST == errno);

    FILE * stream;

    if( (stream = fopen(path.c_str(), "w+" )) == NULL ) // C4996
        printf( "The file was not opened\n" );
    else
        printf( "The file was opened\n" );
    string data = "Hi";
    int numwritten = fwrite( data.c_str() , sizeof( char ), data.length() , stream );
    printf( "Wrote %d items\n", numwritten );
    fclose( stream );
    return 0;
}

If you use it on windows only you should probably use the _mkdir function (as I noted before).

OTHER TIPS

If you can use boost::filesystem (as πάντα ῥεῖ advice) then :

#include <string>
#include <fstream>
#include <boost/filesystem.hpp>

using namespace boost::filesystem;

int main()
{
    int m_iNumOfClass(2);

    path path_template("/tmp"); //Maybe you should use "C:\\tmp" instead
    path_template /= "iter";

    path directory;

    for(int i=0; i<m_iNumOfClass; i++) {

        int directory_index = 0;

        do
        {
            directory_index++;
            directory = path_template;
            directory += std::to_string(directory_index);
        } while (!create_directory(directory));

        directory /= "HMMtransiMean";
        directory += std::to_string(i) + ".txt";

        std::string filename(directory.string());

        std::ofstream outfile (filename);

        outfile.close();

    }

    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top