Skip to main content
gamedev.net
🔒 Locked

Replacing \ character

Started by penetrator Jan 19, 2003 at 1:03 PM 14 replies 700+ views
Original Post
penetrator
penetrator
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);
baumep
baumep
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
Wyrframe
Wyrframe
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.
penetrator
penetrator
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 //
ToohrVyk
ToohrVyk
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
Oluseyi
Oluseyi
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;
}
civguy
civguy
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]
penetrator
penetrator
what headers do i need to use your code ?

civguy
civguy
#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]
penetrator
penetrator
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 ?

civguy
civguy
try \\
zackriggle
zackriggle
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"
penetrator
penetrator
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




Kippesoep
Kippesoep
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.

Kippesoep
penetrator
penetrator
ok, i made some tests and i discovered the GetOpenFileName keep the file open, so when i try to access it again i get the error.
How would i close the file after a GetOpenFileName call ?

penetrator
penetrator
Ok, i found it, with CloseHandle(hFile);


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.