Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Replace all '\' with '/' in a string

This thread is locked; no one can reply to it. rss feed Print
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)

--
sig used to be here

Fladimir da Gorf
Member #1,565
October 2001
avatar

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.
The char *f filename coming in may look like this coming in: "c:\data\maps\map.map"
but when it is copied to the string ff it looks like this: "c:\data"
why would this happen? And does my replace code look about right?

bool cMap::saveMap(char *f)
{
std::string ff = f;

int l = ff.find('\\', 1);
while (l != std::string::npos)
{
if(l != std::string::npos) ff[l] = '/';
l = ff.find('\\', 1);
}

return false;
}

Bob
Free Market Evangelist
September 2000
avatar

If you already have an std::string:

std::replace(str.begin(), str.end(), '\\', '/');

Otherwise, no need to copy:

char *c = strchr(str, '\\');
while (c) {
    *c = '/';
    c = strchr(c + 1, '\\');
}

--
- Bob
[ -- All my signature links are 404 -- ]

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
New String: | s/Michael Dowling/My Documents/temp.map |

C:\temp.map
New String: | C:/temp.map |

C:\WINDOWS\AppPatch\Custom\temp.map
New String: | om/temp.map |

C:\BottomsUp\data\maps\temp.map
New String: | emp.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:

FILE *fi = fopen(f, "wb");
if (fi == NULL) return true;
fclose(fi);

CGamesPlay
Member #2,559
July 2002
avatar

Post the code that calls that function.

--
Tomasu: Every time you read this: hugging!

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

//////////////////////////////////////////////////////////
/*
slashBack.c
*/

#include<stdio.h>

void SlashBack(char *path);

int main(void)
{
char path[100]="c:/folder 1/folder 2/file.txt";

printf("\n\n%s\n%s\n\n\n", "before:", path);
SlashBack(path);
printf("%s\n%s\n\n\n", "after and before 2:", path);
SlashBack(path);
printf("%s\n%s\n\n\n", "after 2:", path);

return 0;
}

/*** FUNCTION *******************************************/

void SlashBack(char path[])
{
int i;

for(i=0; path<i> ; ++i)
{
if( path<i>=='\\')
path<i> = '/';
else if( path<i>=='/')
path<i> ='\\';
}
}

// EOF
///////////////////////////////////////////////////////

----------------------------------------

output:

before:
c:/folder 1/folder 2/file.txt

after and before 2:
c:\folder 1\folder 2\file.txt

after 2:
c:/folder 1/folder 2/file.txt

----------------------------------------

1) learn perfectly ANSI C99
2) understand difference between struct and class
3) try C++
4) write programs in ANSI C99

;)

Arthur Kalliokoski
Second in Command
February 2005
avatar

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
avatar

char *ptr=mystring;
while(*ptr)
{
    if(*ptr=='\\') 
    {
        *ptr='/';
    }

    ptr++;
}

Bob
Free Market Evangelist
September 2000
avatar

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.

--
- Bob
[ -- All my signature links are 404 -- ]

Paul Pridham
Member #250
April 2000
avatar

Oops, for std strings...

for(int=0;i<str.length();i++)
{
    if(str<i>=='\\')
    {
        str<i>='/';
    }
}

Bob
Free Market Evangelist
September 2000
avatar

Quote:

Oops, for std strings...

What's wrong with the code I posted above?

--
- Bob
[ -- All my signature links are 404 -- ]

CGamesPlay
Member #2,559
July 2002
avatar

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?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Bob
Free Market Evangelist
September 2000
avatar

Quote:

What? You mean forward slashes?

Oops. Should be fixed now.

--
- Bob
[ -- All my signature links are 404 -- ]

ReyBrujo
Moderator
January 2001
avatar

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?

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Paul Pridham
Member #250
April 2000
avatar

Quote:

What's wrong with the code I posted above?

Nothin'?

What, only Bob is allowed to post some code? ;) :P

Go to: