Open file by its full path in C++

前端 未结 5 2316
暖寄归人
暖寄归人 2021-02-18 21:24

I want the user to give me the full path where the file exists and not just the file name. How do I open the file this way?

Is it something like this:

if         


        
5条回答
  •  故里飘歌
    2021-02-18 22:17

    For those who are getting the path dynamicly... e.g. drag&drop:

    Some main constructions get drag&dropped file with double quotes like:

    "C:\MyPath\MyFile.txt"
    

    Quick and nice solution is to use this function to remove chars from string:

    void removeCharsFromString( string &str, char* charsToRemove ) {
       for ( unsigned int i = 0; i < strlen(charsToRemove); ++i ) {
          str.erase( remove(str.begin(), str.end(), charsToRemove[i]), str.end() );
       }
    } 
    
    string myAbsolutepath; //fill with your absolute path
    removeCharsFromString( myAbsolutepath, "\"" );
    

    myAbsolutepath now contains just C:\MyPath\MyFile.txt

    The function needs these libraries: .
    The function was based on this answer.

    Working Fiddle: http://ideone.com/XOROjq

提交回复
热议问题