Replacing \ character

Started by
14 comments, last by penetrator 21 years, 2 months ago
try \\
Advertisement
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"
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




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
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 ?

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


This topic is closed to new replies.

Advertisement