Jump to content
  • Advertisement

Archived

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

penetrator

Replacing \ character

This topic is 5756 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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 "\"

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!