mercredi 29 juillet 2020

standard string rfind not finding period

I am trying to extract the name of file, without the extension or qualified path e.g. extract file from /path/to/file.txt

I have the following:

#include <string>
#include <iostream>
using namespace std;

int main()
{
    string temp = "dir/filename.txt";
    auto bpos = temp.rfind('/')+1, epos = temp.rfind('.')-1;
    cout << temp.substr(bpos,epos) << endl;
    return 0;
}

The output is filename.tx and I am not sure why this is the case. Is rfind() just not able to find the period? Escaping the character does not work either, same output.

Aucun commentaire:

Enregistrer un commentaire