|
Replace all '\' with '/' in a string |
MickyD
Member #2,409
June 2002
|
How can I replace all \'s with /'s in an std::string? Here is what I have right now, but I'm pretty sure it's very wrong: int l = ff.find((char *)92, 0); while (l != std::string::npos) { if(l != std::string::npos) ff.replace(l, 0, (char *)92); l = ff.find((char *)92, 0); } I need it because I think a file loading/saving routine is being messed up by a string having \ instead of / |
miran
Member #2,407
June 2002
|
char *fix_filename_slashes(char *path) -- |
Fladimir da Gorf
Member #1,565
October 2001
|
Quote: (char *)92 At least this is wrong. What you might want is string( '\\', 1 ); But use the function miran suggested. OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori) |
MickyD
Member #2,409
June 2002
|
I'm sorry, but I want to use this code. bool cMap::saveMap(char *f) |
Bob
Free Market Evangelist
September 2000
|
MickyD
Member #2,409
June 2002
|
Using the code posted by Bob, I get better results, but still not right yet. I am using the Windows GetSaveFileName dialog and giving the filename returned by that to my save function. Is there a way to set up the dialog to automatically return a filename using / instead of \ slashes? I tried seeing what the code would do to my string with different filenames. The first string is the character string returned from the SaveFileName dialog and the second one is the modified string. When I run the program here is what's recorded in my logfile: C:\Documents and Settings\Michael Dowling\My Documents\temp.map C:\temp.map C:\WINDOWS\AppPatch\Custom\temp.map C:\BottomsUp\data\maps\temp.map EDIT: Maybe I don't need to do all that replacing, but I cant figure this out either. In my save function I could just give it the string "C:\data\maps\temp.map", but what I do it always finds a NULL pointer and doesn't create the file. Here is my code:
|
CGamesPlay
Member #2,559
July 2002
|
Post the code that calls that function. -- Ryan Patterson - <http://cgamesplay.com/> |
Billybob
Member #3,136
January 2003
|
Please tell me you aren't passing fopen an std::string... There's a function called std::string::c_str()
|
marcin
Member #5,814
May 2005
|
////////////////////////////////////////////////////////// #include<stdio.h> void SlashBack(char *path); int main(void) /*** FUNCTION *******************************************/ void SlashBack(char path[]) for(i=0; path<i> ; ++i) // EOF ---------------------------------------- output: before: after and before 2: after 2: ---------------------------------------- 1) learn perfectly ANSI C99
|
Arthur Kalliokoski
Second in Command
February 2005
|
AFAIK, the backslash is only necessary for COMMAND.COM since it uses the forward slash for an option specifier (you can leave out spaces like "c:\utilities screenmode/50") Even in plain DOS, IO.SYS (what the int 21h goes to) c:/utilities/screenmode.exe is a valid path. So why use backslashes at all unless you're doing a system() to command.com? They all watch too much MSNBC... they get ideas. |
Paul Pridham
Member #250
April 2000
|
char *ptr=mystring; while(*ptr) { if(*ptr=='\\') { *ptr='/'; } ptr++; }
---- |
Bob
Free Market Evangelist
September 2000
|
Quote: AFAIK, the backslash is only necessary for COMMAND.COM since it uses the forward slash for an option specifier Unfortunately, there are many Win32 API calls that will not accept forward-slashes as path separators. LoadLibrary() and CreateProcess(), for example. Edit: Had the wrong slash. -- |
Paul Pridham
Member #250
April 2000
|
Oops, for std strings... for(int=0;i<str.length();i++) { if(str<i>=='\\') { str<i>='/'; } }
---- |
Bob
Free Market Evangelist
September 2000
|
Quote: Oops, for std strings... What's wrong with the code I posted above? -- |
CGamesPlay
Member #2,559
July 2002
|
Bob said: Quote: AFAIK, the backslash is only necessary for COMMAND.COM since it uses the forward slash for an option specifier Unfortunately, there are many Win32 API calls that will not accept back-slashes as path separators. LoadLibrary() and CreateProcess(), for example. What? You mean forward slashes? -- Ryan Patterson - <http://cgamesplay.com/> |
Bob
Free Market Evangelist
September 2000
|
Quote: What? You mean forward slashes? Oops. Should be fixed now. -- |
ReyBrujo
Moderator
January 2001
|
If I recall correctly, forward slashes are used to load different interfaces. Like LoadLibrary("C:\\Winnt\\System32\\sscrt.dll/2"); loads the second interface of the sscrt.dll... or were semi colon needed? -- |
Paul Pridham
Member #250
April 2000
|
Quote: What's wrong with the code I posted above? Nothin'? What, only Bob is allowed to post some code? ---- |
|