I have a program where it starts by asking the user to input the data path. Although the prompt provides the example (c:/folder), being windows users the program is frequently given the response of c:\folder. This means that the compiler treats that data path string as if it contains a \f control character. This becomes even more problematic if the folder name is something like "test" so that the path is "c:\test" and the compiler interprets that string as "c: 'tab' est"
So, how do I parse out the character '\' from the string and replace it with a '/'?
I guess I need to clarify.
I use the path that the user provides to create several input file names that will be read. So, if path = "c:/folder" then file1name would be created as "c:/folder/file1.txt" and file2name would be created as "c:/folder/file2.txt." So I am saving path as a string variable and creating filename strings as path + '/' + filename. When the windows user inputs to path "c:\test" and this gets stored in the string path, when I do filename=path + '/' + file1name and print out the result, I get: c:<tab>est/file1.txt.
This newly created filename, which should be c:\test/file1, can be submitted to file.open(filename, std::fstream::in); But when this is tested as file.is_open() the outcome is always false.
So this is way I want to replace '\' with '/' in the path string.
However, if there is a different approach to this issue that does not involve char replacement within the string, then I would like to know that too.
What I have tried:
I know that backslash is properly represented as '\\' and if the user provided that as input there would be no problem. However the double slash is not the windows format.
Everything I tried treats "\test" as "control-t est"
Is there someway I can get C++ to treat \ as an isolated character within a string?
I suppose I could test the string for every character preceded by a '\' (as in '\a' through '\z'), but that seems a very brute force solution. Is there something a little more elegant?