terça-feira, 11 de fevereiro de 2014

C++ - How to get the filename and directory path

Usando o método contido na classe string
Using the method from string class

code:
#include <iostream>
#include <string>

using namespace std;

int main() {

        string fullPath = "/home/guest/test.txt";
        string path = fullPath.substr( 0, fullPath.find_last_of("/\\") );
        string filename = fullPath.substr( fullPath.find_last_of("/\\")+1 ); 

        cout << "fullPath: " << fullPath << endl;
        cout << "path....: " << path     << endl;
        cout << "filename: " << filename << endl;

        return 0;

}

para compilar:
    g++ -o teste teste.cpp

result:
fullPath: /home/guest/test.txt
path....: /home/guest
filename: test.txt


Reference: http://www.cplusplus.com/reference/string/string/find_last_of/

Nenhum comentário:

Postar um comentário