Resolving correct file paths with whitespaces

I'm working on a file browser and have come up against an issue with resolving file paths that are retrieved from a vector of strings.

If the true path to a file is
"C:\Users\Public\New Text Document.txt" 

the program requires
"\"C:\\Users\\Public\\New Text Document.txt\""

in order to open it.

But the path and filename are not static, but vary with the situation involved, so I need to properly automate this conversion.

A basic example of what I'm referring to is:
1
2
3
        std::string filename="\"C:\\Users\\Public\\New Text Document.txt\"";
        const char *filename2=filename.c_str();
		system(filename2);

This works but requires me to personally initialize the filename's path.

How could I get THIS to work as it would be in the same format as the retrieved strings in my vector?
1
2
3
4
std::string filename="C:\Users\Public\New Text Document.txt";
//HOW DO I CONVERT THE REAL PATH TO THE CORRECT PATH C++ UNDERSTANDS?
        const char *filename2=filename.c_str();
		system(filename2);


EDIT: I know, use of system is not recommended. It's merely for a simplified example.
Last edited on
Maybe I'm overthinking this. It could be that because the string is already populated with the appropriate "\" in each space when retrieved from the vector that it will work fine and whitespaces will be fine just by adding quotes to the path string before processing. I'll have to try it.


UPDATE:

It works. I need to create another string and have it assigned the value of "\"", then add my retrieved vector string and add an additional "\"" on the end. Then when the system call occurs using the new string, it loads correctly.
Last edited on
Topic archived. No new replies allowed.