-4

Possible Duplicate:
Splitting a string in C++

I have a program that copies files.

I have a string which is a directory path, however it could be just a file name. For example:

rootdirname\childdirname\filename.ini

or it could be:

filename.ini

Im still quite new to C++, I need to split the string on \ and create directories with MKDir.

Any one know how to split the string??

Community
  • 1
  • 1
JP29
  • 633
  • 4
  • 13
  • 25
  • 4
    How hard did you look? Duplicate: [Splitting a string in C++](http://stackoverflow.com/questions/236129/splitting-a-string-in-c), [How do I tokenize a string in C++?](http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c?lq=1), and many more. – Jesse Good Jul 18 '12 at 08:11
  • string is a char array, so iterate through the array until you come accross '/' and start concatenating chars from here in a temporary char array, every time you come accross a new '/' clear the temporary, in the end you will have filename.ini in the temporary char array. or just start from the and and iterate backwards and take the first temporary array without clearing it – huseyin tugrul buyukisik Jul 18 '12 at 08:13
  • and you can look into strtok library to get the idea – huseyin tugrul buyukisik Jul 18 '12 at 08:15

3 Answers3

1

I'm not sure how you are defining your string but if it is a char* you can use strtok. http://www.cplusplus.com/reference/clibrary/cstring/strtok/

Chefire
  • 139
  • 1
  • 7
0

You can use this C solution:

http://www.cplusplus.com/reference/clibrary/cstring/strtok/

This allows you to split a string in many tokens. You slip the string on "/" like you want.

There is also this answer here in Stackoverflow which I think it will help:

How do I tokenize a string in C++?

Community
  • 1
  • 1
petermlm
  • 930
  • 4
  • 12
  • 27
  • There used to be issues with strtok() - saved state in a static that was not thread-safe. Maybe things have changed, but..? – Martin James Jul 18 '12 at 09:08
0

reinvented the wheel on linux

#include <string>
#include <sys/statfs.h>

bool existsDir( const std::string& dir ) {
    return existsFile( dir );
}

bool existsFile( const std::string& file ) {
    struct stat fileInfo;
    int error = stat( file.c_str(), &fileInfo );
    if( error == 0 ){
        return true;
    } else {
        return false;
    }
}

bool createDir( std::string dir, int mask = S_IRWXU | S_IRWXG | S_IRWXO ) {
    if( !existsDir( dir ) ) {
        mkdir( dir.c_str(), mask );
        return existsDir( dir );
    } else {
        return true;
    }
}

bool createPath( std::string path, int mask = S_IRWXU | S_IRWXG | S_IRWXO ) {
    if( path.at( path.length()-1 ) == '/' ){
        path.erase( path.length()-1 );
    }
    std::list<std::string> pathParts;

    int slashPos = 0;
    while( true ) {
        slashPos = path.find_first_of( "/", slashPos+1 );
        if( slashPos < 0)
            break;
        std::string pp( path.substr( 0, slashPos ) );
        pathParts.push_back( pp );
    }
    std::list<std::string>::const_iterator pp_cit;
    for( pp_cit=pathParts.begin(); pp_cit!=pathParts.end(); ++pp_cit ){
        createDir( (*pp_cit), mask );
    }

    createDir( path, mask );
    return existsDir( path );
}
cygenb0ck
  • 53
  • 5