delete file on Windows

Started by
16 comments, last by FGFS 8 years, 10 months ago

Put differently:

Certain values are difficult or impossible to place inside strings.

Some examples:

You cannot use a text file to store the backspace character directly. Attempting to enter a backspace will erase the character before it. Instead use the sequence \b

You cannot directly store a quotation mark because that is the mark that indicates the end of a string. Instead use the sequence \"

You legally cannot end a line inside a string in C++. To make a multi-line string you can use the sequence \n

Since the backslash character is used as an escape sequence, it needs to also be escaped. To represent a backslash use the sequence \\

Any time you have a backslash in a string or character block, the backslash must be followed by another value.

C++ specifies the following escape codes:

\" double quote
\' single quote
\? question mark
\\ backslash
\a alert (console beep)
\b backspace
\f form feed (on printers, end the current page)
\n newline
\r carriage return
\t horizontal tab (usually implemented as aligning to 8 spaces)
\v vertical tab (usually implemented as several new lines)
Then there are some other special characters:
\## Direct number in octal format ( \042 is the ascii glyph double-quote, \0141 is the ascii glyph a)
\x## Direct number in hex format ( \x22 is the ascii glyph double-quote, \x61 is the ascii glyph a)
The compiler may add additional codes.

Looking over your line that uses "\\\"), 1, "\\"); when the compiler examines it, it interprets the string as: Start of string, special character backslash, special character quote symbol, parenthesis, comma, space, one, comma, space, end of string.

Obviously that is not what you intended.

Advertisement

Thanks. Still no luck replacing \\ with \ in my path.

Thanks. Still no luck replacing \\ with \ in my path.

And because this is For Beginners, we try again...

Assuming you want help, please provide EXACT error messages. Without them we can only guess at the true error.

What is the value returned by the function?

For the windows variant, after it fails, what is the return value of GetLastError()?

For the unix variant, after it fails, what is the output of perror()?

What are the exact contents of the string?

Show each in a debugger screenshot if you are not 100% certain about the results, or the contents and escaped values.

Your code should be checking those error values. The fact that it does not is a defect in your code.

For example:

BOOL status = DeleteFile(InFileLocHw.c_str());

if(status) {

DWORD weNeedThisValue = GetLastError();

}

Or if you are doing the other one:

int status = remove(InFileLocHw.c_str());

if(status) {

perror(InFileLocHw.c_str()); /* Look in your output window, copy/paste the message it spits out */

}

Well I don't need that anymore, so just out of curiosity. It doesn't compile:

#if IBM
InFileLocHw.replace(InFileLocHw.find("\\\"), 1, "\\");

...

error output:

../main.cpp:1257:3: error: stray ‘\’ in program
InFileLocHw.replace(InFileLocHw.find("\\\"), 1, "\\");
^
../main.cpp:1257:3: error: stray ‘\’ in program
../main.cpp:1257:54: warning: missing terminating " character
InFileLocHw.replace(InFileLocHw.find("\\\"), 1, "\\");
^
../main.cpp:1257:3: error: missing terminating " character
InFileLocHw.replace(InFileLocHw.find("\\\"), 1, "\\");

Thanks

Again:

If you want a string containing "\\" you need to use "\\\\" in the source. If you want a string containing "\" you need to use "\\" in the source.


"\\\" in C++ is completely broken though. It means a string containing a \, followed by " and then the string ends without being ever probably terminated, hence the compile errors.

InFileLocHw.replace(InFileLocHw.find("\\\"), 1, "\\");

You have to think like a compiler, character by character. In the above line of code:

Expected form for the find function: find ( <string sequence> ) - i.e., the word find, an open parentheses, a string sequence, a close parentheses.

find - function to call - okay. Open parentheses should be the next character.

( - expected open parentheses character - okay. Next character should be the beginning of a string.

" - string start character double-quotation-mark - okay. Add characters to the string until the string end double-quotation-mark <"> is found

\ - interpret and insert next character into the string

\ - inserted into the string

\ - interpret and insert next character into the string

" - inserted into the string <-- N.B., this is NOT interpreted as the end of the string

) - inserted into the string

, - inserted into the string

1 - inserted into the string

, - inserted into the string

" - double-quotation-make end of the string - okay.

\\ - find function expects a close parentheses but it's not there. Instead there's an escaped <\> Result --> "stray \" error

EDIT: Following that sequence, there is just a single double-quotation-mark indicating the start of a string. But there is no string end double-quotation-mark --> missing terminating " character.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I highly recommend using an IDE or text editor with good syntax coloring, since it will be painfully obvious whether you've closed a string properly or not, and you'll get feedback as you're typing.

Every single backslash you want to put in a string needs to be doubled. When \ is used as an escape character, it only 'escapes' one character after it. So if you have multiple characters you need to escape, you need a new \ to escape each one:

"\\" is a string with a single backslash.
"\\\\" is a string with two backslashes.
"\\\\\\" is a string with three backslashes.

Thanks Nypyren, you're right. This compiles:

InFileLocHw.replace(InFileLocHw.find("\\\\"), 1, "\\");

I'm currently on Eclipse/Ubuntu but I try next time I boot Windows.

This topic is closed to new replies.

Advertisement