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).