By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

removing files

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Fri, 11 Jul 2003 10:39:08 +0200, Anthony wrote:
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");
}

--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}
Nov 13 '05 #2

P: n/a
"Anthony" <An*****@work.nl> wrote (11 Jul 2003) in news:bem06m$6dt$1
@sunrise.wldelft.nl / comp.lang.c:
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.


--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America
Nov 13 '05 #3

P: n/a
On Fri, 11 Jul 2003, Anthony wrote:
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);
}
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


--
main(){int j=1234;char t[]=":@abcdefghijklmnopqrstuvwxyz.\n",*i=
"iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}

Nov 13 '05 #4

P: n/a
On 11 Jul 2003 13:54:10 GMT, da*****@cs.toronto.edu (Darrell Grainger)
wrote in comp.lang.c:
On Fri, 11 Jul 2003, Anthony wrote:
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);
}


The standard does not require the remove function to put a value in
errno.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.