removing files

A

Anthony

Hi,

Removing files seems to be rather clumsy in C (not C++).

I am trying next piece of code to delete a file (under W32);

int errno;
errno = remove("FSelCancelled");

After exiting the app the file is still on the system, so not deleted at all.

If I check the return code, I see value for errno = -1.

On the several doc. pags for remove I see return codes 7 (EBIG), 13 (EACCESS) , etc, etc. till 18 (EXDEV), but not an error code -1.

So, what's wrong ?

Thanks.
Anthony
 
P

Pieter Droogendijk

Hi,

Removing files seems to be rather clumsy in C (not C++).

I am trying next piece of code to delete a file (under W32);

int errno;
errno = remove("FSelCancelled");

After exiting the app the file is still on the system, so not deleted
at all.

If I check the return code, I see value for errno = -1.

On the several doc. pags for remove I see return codes 7 (EBIG), 13
(EACCESS) , etc, etc. till 18 (EXDEV), but not an error code -1.

So, what's wrong ?

Thanks.
Anthony

You're mixing up return value and error code. I'll quote the man page:
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and
errno is set appropriately.

if remove() returns -1, check errno. You can use perror() to print the
last error in a human-readable way. These human-readable strings are
stored in *sys_errlist[], defined in errno.h, if you have something
against perror().

#include <stdio.h>
int main ()
{
if (remove ("somefile") < -1) perror ("removing file");
else printf ("i'm happy\n");
}
 
M

Martin Ambuhl

Hi,

Okay, thanks for that info.

Small problem that still remains, is that I have a GUI based
app, what means that I don't see the printed error message
from the perror function.

Is there any way to use a proper function, returning an
error string telling me what's wrong.

strerror().
I can't believe that someone who doesn't know about perror and
strerror has the gall to post
Removing files seems to be rather clumsy in C (not C++).>
Knowing something about C should be a requirement for people who want
to complain about it.
 
D

Darrell Grainger

Hi,

Removing files seems to be rather clumsy in C (not C++).

I am trying next piece of code to delete a file (under W32);

int errno;
errno = remove("FSelCancelled");

After exiting the app the file is still on the system, so not deleted at all.

If I check the return code, I see value for errno = -1.

You overwrote the errno variable with the result of remove(). Try this:

int result;
result = remove("FSelCancelled");
if(result == -1) {
printf("errno: %d\n", errno);
}
 
J

Jack Klein

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,829
Latest member
PIXThurman

Latest Threads

Top