Archived

This topic is now archived and is closed to further replies.

penetrator

Replacing \ character

Recommended Posts

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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 //

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 example


const 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;
}

Share this post


Link to post
Share on other sites
If algorithms produce longer and more complicated code, don't use them

      
std::string text = "C:\\path\\to\\file"; // for example


const 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]

Share this post


Link to post
Share on other sites
#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]

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
You get a "newline in constant error" because of the \s. Like EVERYONE else said, if you write ANYTHING in the code and you mean it to turn out a slash "\", you need to write "\\" in the code. There is no character assigned to \s, so the compiler assumes that you were going to continue the string on the next line.

Ex:
(example)
char mystring[] =
" Hello. My name\
is Bob. What is your\
Name?"
(example)

Would effectively produce the string:
" Hello. My name is Bob. What is your Name?"

You told the compiler that you wanted to continue the string on the next line, but you did not. Anyways, it thinks you want to continue the string on the next line, so it reads it as a string. When it got to the end of the line, there was no "\" to tell it to keep going, nor was there a " to tell it that it had reached the end of the string. So, it gives you the error message.

One again:
The string "He\llo" DOES NOT MAKE "He\llo"!
This does: "He\\llo"

Share this post


Link to post
Share on other sites
Ok, i got it, excuse me for being so stubborn.
So, if finally i understood it, when i run this function:

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

the name file is stored into ofn.lpstrFile . If i use a printf i see, for example, "C:\test.txt" , but the "real" value is "C:\\test.txt".
So, later i want to open this file, so i

FILE *pFile;
pFile = fopen( ofn.lpstrFile, "r" );

If i run that, i get an access violation, while if i

pFile = fopen( "C:\\test.txt", "r" );

it works fine. But shouldn''t ofn.lpstrFile hold the same identical value ?

Excuse me again ... i''m trying to learn




Share this post


Link to post
Share on other sites
No, the "real" value is still "C:\test.txt". The \\ is only used in your source code, so the compiler can translate it correctly. If you get an access violation in your code, then something must have changed the value of that pointer.

Share this post


Link to post
Share on other sites