delete file on Windows

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

Hi

why does this not work on Windows? Not even an error, also path is correct.(G:\\...)

//remove(InFileLocHw.c_str());
DeleteFile(InFileLocHw.c_str());

The remove line works in Linux.

Thanks in advance.

Advertisement

(G:\\...)


If that path is actually contained in the string, it is most likely wrong. You have to escape the backslash when you write the string in the source file (unless you can use one of the nifty string features in newer C++ versions) not in any string. Also note that a lot of Windows functions can deal with '/' instead of '\' as the path separator.
Edit: To make it slightly clearer:



std::string test1 = "G:\\test.txt";
// test1 now contains the string "G:\test.txt"
std::string test2 = readFromMyFile();
// if test2 now contains "G:\\test.txt" then this is wrong for further file operations
Assumining the readFromMyFile function just does a straight forward read, the file (or similar source) has to contain a line like
G:\test.txt
End Edit.

Also, note that DeleteFile returns 0 if there is no error. If there is an error you need to call GetLastError to retrieve a more detailed error code.

When remove fails it'll return -1/nonzero value and set errno, what the errno (perror) says could help identify the problem.

Thanks. Why do I get

error. missing closing quote

#if IBM
InFileLocHw.replace(InFileLocHw.find("\\"), 1, "\");
//std::replace(InFileLocHw.begin(), InFileLocHw.end(), '\\', '\');
#endif

Thanks again

Thanks. Why do I get

error. missing closing quote

#if IBM
InFileLocHw.replace(InFileLocHw.find("\\"), 1, "\");
//std::replace(InFileLocHw.begin(), InFileLocHw.end(), '\\', '\');
#endif

Thanks again

InFileLocHw.replace(InFileLocHw.find("\\"), 1, "\"); <- You have double quotes around the last backslash so it thinks you are trying to do an escape sequence. Try wrapping it in single quotes instead.

No luck with single quotas. Would be interesting to know.

Anyway not the \\ was the problem, but that I didn't close the stringstream handle

prior to delete. Crossplatform is where you find the bugs. wacko.png

Thanks


Crossplatform is where you find the bugs.

The real issue with it dates back to the 1970s and the choice to use the backslash (\) as the DOS path delimiter when the backslash was also a common string escape symbol. Other disk systems used a forward slash (/) as the delimiter.


Why do I get
error. missing closing quote

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


Anyway not the \\ was the problem,

In that code snippit, that is the problem causing the compile error. You've got two escaped values.

"\\" means start quote, then two backslashes (the backslash is an escape code to trigger a special value, the next backslash is your special value that inserts a backslash character), then an ending quote that terminates the string.

"\" means a start quote, then a backslash and quote (the backslash is an escape code to trigger a special value, the quote is your special value that inserts a quote character rather than ending the string). There is no string terminator.

As for the remove() versus DeleteFile(), both functions provide return codes. You should be capturing those return codes. Get in the habit of capturing and testing all error codes. While it is frequently left out of (bad) online tutorials and sample code, in The Real WorldTM you need to check the results of all your operations and take some action if anything went wrong.

In both of these cases, anything other than zero means there was an error. Capture that error code.

As others mentioned, if remove() fails use you can use perror() to print out a friendly error message to the console. If DeleteFile() files use GetLastError() to capture the error code and FormatMessage() to create a friendly error message you can print. Both need to be called IMMEDIATELY after the failure, as many functions share the error code and another system call can overwrite the last error code.

Thanks, that's what I thought as \ is escape on Unix. Still this doesn't work:

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

Thanks, that's what I thought as \ is escape on Unix. Still this doesn't work:

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

That's because of the "\\\" part. Remove one of the slashes.

Thanks, that's what I thought as \ is escape on Unix. Still this doesn't work:

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


That has nothing to do with Unix. C++ treats \ as an escape character in quoted strings. 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.

This topic is closed to new replies.

Advertisement