How to remove the double quotes (") at the end of the process name in C++? RRS feed

  • Question

  • Hello All,

    I am using the below code snippet to get the process name (powerchute.exe) from the given path

    std::wstring strProcessName;
    std::wstring strFilePath = "C:\Program Files (x86)\Common Files\Installer\powerchute.exe";
    size_t npos = strFilePath.rfind(L"\\");

    strProcessName = strFilePath.substr(npos + 1, strFilePath.length());

    But it is showing double quotes (") with process name (powerchute.exe"). How to remove double quotes (") showing at the end of the process name?


    Tuesday, November 12, 2019 5:15 PM


  • The hint is the second parameter of substr.

    The specification for substr states that the first parameter of substr is the starting position, where the second parameter is the size of the substr. Since you are currently passing in the length of the string then it is bounding it to the end of the string.

    So you simply work out where you want the substring to end and then use that as the second parameter to substr.

    As a hint:

    size_t lastpos = strFilePath.rfind(L"\"");

    Will get the position of the last quotation mark. You can then work out the difference between the two positions to get the size of the string.

    Also, when you post code please post code that compiles. Assigning a narrow string literal to a std::wstring results in a compiler error. Also you can't have a single \ in the string literal. You need to escape it like you did in your rfind.

    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Tuesday, November 12, 2019 5:44 PM