Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Updated 20-Jan-22 8:10am
v2
Comments
Richard MacCutchan 20-Jan-22 12:49pm    
I just tried that and it works fine. When you say "the compiler treats that data path string as if it contains a \f control character", the compiler is not seeing data that a user types into your running application.
Rick York 20-Jan-22 13:17pm    
I often replace backslashes with forward slashes for similar reasons. The thing is the OS and API can handle forward slashes just fine. They work exactly like backslashes. From what I've found the only thing that doesn't handle them correctly is the windows command prompt. It thinks forward slashes are argument delimiters. So, while you don't have to replace them, you can if you want and I usually use a loop around the strchr function to do it. It returns a pointer to the first occurrence of the specified string so it's a simple matter to replace that with a different character.
T Bones Jones 20-Jan-22 15:18pm    
Maybe its my compiler, I am using Microsoft Visual C++ 2019.
Or my operating system, Windows 10

When I save a string like "This is \t my \g string" it gets treated, and prints out, as "This is <tab> my g string". So, what I am getting is either a control character, \t as tab, or the blackslash is ignored, \g as g.

Interestingly, I have printed the string out character by character. When it gets to \t the \ prints as an empty space and then next character is printed as a tab. When it gets to \g only the g is counted and printed. It is as if the \ did not exist.

1 solution

No, it doesn't - '\' is only "obeyed" when constructing a string in your source code - it's the compiler that interprets the backslash as an escape sequence introducer which requires '\\' to insert a single backslash character.

When the user types a path "C:\folder\test.dat" that is exactly what you get: 18 characters including two backslashes, not 16 including a '\f' and a '\T' character.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900