lundi 29 février 2016

C++ File System

I am trying to add these functions.

std::string GetDirectory(std::string path_name) {
    boost::filesystem::path path(path_name.c_str());

    path_name = path.string();

    path_name.erase(
        path_name.substr(
            path_name.find_last_of(DIRECTORY_SEPARATOR),
            path_name.length()
        )
    );
    return path_name;
}

std::string GetName(std::string path_name) {
    boost::filesystem::path path(path_name.c_str());

    path_name = path.string();

    path_name.erase(
        path_name.substr(
            0, 
            path_name.find_last_of(DIRECTORY_SEPARATOR)
        )
    );

    path_name.erase(
        path_name.substr(
            path_name.find_last_of("."), 
            path_name.length()
        )
    );
    return path_name;
}

std::string GetExtension(std::string path_name) {
    boost::filesystem::path path(path_name.c_str());

    path_name = path.string();

    path_name.erase(
        path_name.substr(
            0, 
            path_name.find_last_of('.')
        )
    );
    return path_name;
}

I am trying to add:

C:\Windows\Users\Example\Desktop\test.txt

GetDirectory

C:\Windows\Users\Example\Desktop

GetName

test

GetExtension

txt

I get errors though between path_name and erase (the period). Help would be much appreciated, Thank you.

Aucun commentaire:

Enregistrer un commentaire