How to find \ character into a string ?

Jun 8, 2011 at 10:41am
I've write :
string.find("\\") to locate the 'folder'character (windows style) separator but without success.
Any idea ?

mmm
The problem was I'm passing the string so :
MY_function ("d:\my_folder\my_subfolder");
And the string received was strange (I suposse because I can not write d:\my_folder\my_subfolder, I have to write d:\\my_folder\\my_subfolder.

Is there any way to say my ide + compiler that "d:\my_folder\my_subfolder" is valid ?
Thanks
Last edited on Jun 8, 2011 at 11:06am
Jun 8, 2011 at 11:33am
No. No compiler will interpret that string literal the way you want.

By the way, Windows understands both '\\' and '/' as directory separators, so you don't need to use backslash in your source.
Jun 8, 2011 at 2:05pm
+1 helios

Just use '/'
Jun 8, 2011 at 2:34pm
-1 to use "/" without discrimination. While you can input a filename using the slash (/), the Windows API will output filenames with backslah (\). If you need to break the path into folder names + file name, suggesting the slash is a mistake.
Jun 8, 2011 at 2:40pm
We're talking about string literals.
Jun 8, 2011 at 2:42pm
+1 to use "/" without discrimination.

If you need to break the path into folder names + file name, suggesting the slash is a mistake.

How so?
Jun 8, 2011 at 2:44pm
GetModuleFileName() returns a path + filename. If you were to parse that for folder names, you must find backslashes, not slashes.

EDIT: This is probably the reason why the .Net framework includes System.IO.Path: So managed code can be written in a portable manner when manipulating filenames.
Last edited on Jun 8, 2011 at 2:46pm
Jun 8, 2011 at 2:47pm
Actually, you need to find both.
Jun 8, 2011 at 2:49pm
How come both? I have never seen a filename output from a Windows API function call that includes both. Or do you mean that you need to find both if you want the code to remain portable. Is that it?
Jun 8, 2011 at 2:52pm
No, this isn't about portability. In Windows, both / and \ are valid path separators, so if you're breaking up a path into its components, you must account for both.
Jun 8, 2011 at 2:53pm
Under windows you can use the splitpath() function.

string.find("\\") to locate the 'folder'character (windows style) separator but without success.
Do you know what's the string is when you're trying to find the backslash? If the string is really this "d:\my_folder\my_subfolder" you cannot find the backslash since it's not there.
Jun 8, 2011 at 2:55pm
I understand that you can input string literals that represent a filename and path to Windows using either the backslash or the slash. That is not under discussion. The problem that I point out is to think you can expect slashes from calls to functions that return file names, like GetModuleFileName(). Those will always return backslashes.
Jun 8, 2011 at 2:56pm
No, this isn't about portability. In Windows, both / and \ are valid path separators, so if you're breaking up a path into its components, you must account for both.
For input to the API, yes, but I'm pretty sure no API function will return a forward slash in the middle of a path, unless the string comes from the registry or something like that.
Jun 8, 2011 at 3:00pm
@coder777: Where is that function? It's the first time I ever hear about splitpath().
Jun 8, 2011 at 3:04pm
It might be a Visual Studio thing look at this: http://msdn.microsoft.com/en-us/library/e737s6tf.aspx
Jun 8, 2011 at 3:05pm
The WinAPI needs to pick a default when constructing paths and this default is the backslash.
I don't know where you see a problem. If you're worried about someone adding parts containing slashes to a path returned by the WinAPI: you can safely mix slashes and backslashes in the same path.
Jun 8, 2011 at 3:06pm
It's a VC++ C library extension, not part of the WinAPI.
Jun 8, 2011 at 3:06pm
the Windows API will output filenames with backslah (\).


I would wrap those calls and convert the output strings to use forward slash.

Nearly every other directory tree in the world uses /. It's pretty much universal except for Windows. But even other trees that you might use on Windows use it (like file archives.. such as .zip, etc)
Topic archived. No new replies allowed.