Removing double quotes from Cstring

Hi, sorry its late...

I am trying to do a few things with strings.

- I need to remove double quotes from a users input
- Then the string needs to be divide into two separate strings. So that I have a folder path and a complete file path.


Here is part of my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  
    char sztmp[1024];
    char sztmp_ROM[1024];
    const char* filepath = " ";
    const char* filepath_ROM = " ";


    cin.clear();
    SetConsoleTextAttribute(hConsole, 9);
    cout << "\nEnter Directory of your File:\n " << endl;
    cin.getline(sztmp_ROM, sizeof(sztmp_ROM), '\n');
    filepath_ROM = sztmp_ROM;

    CString path(sztmp_ROM);
    path.Truncate(path.ReverseFind('\\'));


    // Create Sub folders
    string combined_file_path(string(path) + "\\Diddy");
    CreateDirectory(combined_file_path.c_str(), NULL);
Last edited on
Why use CString and char arrays and not std::string?

Perhaps sample input(s) and expected output would make it clear what's required.
+1 for sample input / output
seeplus thanks for your suggestion, I am now using an std::string.
The following will remove the double " quotes from a string and finds the file path without the file name. for example C:\\path_of_file\\ instead of C:\\path_of_file\\file.bin

1
2
3
4
5
6
7
8
9
10
11
cout << "\nEnter Directory of your file:\n " << endl;
    cin.getline(sztmp_ROM, sizeof(sztmp_ROM), '\n');
    std::string directory(sztmp_ROM);
    directory.erase(remove(directory.begin(), directory.end(), '\"'), directory.end());

    string path;
    const size_t last_slash_idx = directory.rfind('\\');
    if (std::string::npos != last_slash_idx)
    {
        path = directory.substr(0, last_slash_idx);
    }
Last edited on
Perhaps it would be best to use std::filesystem::path for this?
Last edited on
Since C++20 you can do this for L4:

 
erase(directory, '\"');


You can also do this:

1
2
    if (const auto last_slash_idx {directory.rfind('\\')}; std::string::npos != last_slash_idx)
        path = directory.substr(0, last_slash_idx);

If the user is using double-quotes, it is because he was pressing TAB to get auto-complete at the command line, which presumably added the double-quotes to the filename because the file name contains one or more of the following special characters and/or any whitespace:

    & < > [ ] | { } ^ = ; ! ' + , ` ~  

Filenames themselves are not supposed to contain double-quotes. (There are very tricky ways around that, but I think it unlikely to appear in your valid input. Windows itself won’t know how to handle such a filename.)

The best way to handle this is to use a std::filesystem::path object. Assign it the (possibly quoted) filename. You will get a correct result when you use the path to open the file. You also get a slew of convenient functions to split the path into directory and filename. As always, the documentation is your friend.

https://en.cppreference.com/w/cpp/filesystem/path


I do NOT recommend trying to parse any of this yourself, either using std::quoted or messing with searching for path separatorS. Let the path object do all that for you. It will get it correct where stuff we try to hack up will fail when you least expect it.
Thanks for your advice.

I myself don't get the double quotes even pressing TAB. This user even took a video showing the quotes.

You only get double quotes if some component of the filename path has a special character in it.
Registered users can post here. Sign in or register to post.