Replacing \ character

Started by
14 comments, last by penetrator 21 years, 3 months ago
If i store a filename path into a string, later i have to switch the \ char into // , so i run a routine to search and replace \ with // When i do that, i get a compiler "newline in constant".This is my code: char * found = 0; char formed[60] = {0}; const char text[60] = "c:\morrison.dem"; const char * to_replace= "\"; found = strstr( text, to_replace); if( found != 0 && found != text ) { strncpy( formed, text, (found - text) ); strcat( formed, "//" ); strcat( formed, (found + 1) ); } printf(formed);
Advertisement
Change the two corresponding lines in your program to:

const char text[60] = "c:\\morrison.dem";
const char * to_replace= "\\";


The compiler will replace "\\" with a single "\"
baumep
Change...
const char * to_replace= "\"; 

To...
const char * to_replace= "\\"; 


Remember that \n is translated as a newline? Well, \" is translated as a double-quote. \\ is translated as the \ character. Also, that \m in "c:\morrison.dem" is gonna get turned into "c:?orrison.dem", with the ? as some wierd character... turn it into "c:\\morrison.dem" as well.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
baumep, i meant that the filename is stored into a char variable via a GetOpenFileName function, like this:

if (GetOpenFileName(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
strcpy(char text,ofn.lpstrFile);

So, char_text is "c:\morrison.dem"
I have to convert those \ to //
Data returned by the function :
c : \ m o r r i s o n . d e m ENDSTRING

What you write in the SOURCE CODE :
"c:\morrison.dem "

And what your source code gets TRANSLATED to, once compiled :
c : ? o r r i s o n . d e m ENDSTRING

This doesn''t really look like the data returned by the function...

What I write in the SOURCE CODE :
"c:\\morrison.dem"

What this gets TRANSLATED to, once compiled:
c : \ m o r r i s o n . d e m ENDSTRING

So my solution works, yours doesn''t...

Note : compilers such as VC6 give out a C4129 warning and simply ignore the \, effectively compiling the source code : "c:morrison.dem"

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!
Click here to learn more
You wish to "replace" one character with two; strictly speaking, that''s not possible. What you really have to do is create a new string, extract all the characters up to the one you wish to replace and put them in the new string, append the characters you want to replace with, and then append the rest of the string, applying the same logic to that sub-string.

C++ isn''t the most elegant of text manipulation solutions.

  std::string text = "C:\\path\\to\\file"; // for exampleconst char replace = ''\'';std::string replace_with = "//";std::string newtext; int p0 = 0, p1 = 0;while( p0 != std::string::npos ){  p0 = text.find_first_of( replace );  if( p0 != std::string::npos )  {    newtext += text.substr( p1, p0 - p1 );    newtext += replace_with;  }  p1 = p0;}  
If algorithms produce longer and more complicated code, don't use them

      std::string text = "C:\\path\\to\\file"; // for exampleconst char replace = '\\';std::string replace_with = "//";std::string newtext; for (std::string::const_iterator i = text.begin();     i != text.end(); ++i) {  if (*i == replace)    newtext += replace_with;  else    newtext += *i;}      


[edited by - civguy on January 19, 2003 3:47:59 PM]
what headers do i need to use your code ?

#include <string>

And if you don't want to type std::string all the time, put
using std::string;
on top of your cpp file and just type string. Like this:


        #include <string>using std::string;int main() {  string text = "C:\\path\\to\\file"; // for example    const char replace = '\\';  const string replace_with = "//";  string newtext;     for (string::const_iterator i = text.begin();       i != text.end(); ++i) {    if (*i == replace)      newtext += replace_with;    else      newtext += *i;  }}  



[edited by - civguy on January 19, 2003 6:54:02 PM]
anyway, what i dont understand, if i have a string like this:

char mystring[10]="my\string";

ho to get rid of the \ and have a new string like

char mystring2[10]="mystring";

I wrote a si,ple routine that check every char, but the compiler says error C2001: newline in constant

can be that difficult ?

This topic is closed to new replies.

Advertisement