removing files

Discussion in 'C Programming' started by Anthony, Jul 11, 2003.

  1. Anthony

    Anthony Guest

    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
    Anthony, Jul 11, 2003
    #1
    1. Advertising

  2. 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;}
    Pieter Droogendijk, Jul 11, 2003
    #2
    1. Advertising

  3. "Anthony" <> 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
    Martin Ambuhl, Jul 11, 2003
    #3
  4. 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[]=":mad: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;}
    Darrell Grainger, Jul 11, 2003
    #4
  5. Anthony

    Jack Klein Guest

    On 11 Jul 2003 13:54:10 GMT, (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
    Jack Klein, Jul 12, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ion
    Replies:
    2
    Views:
    437
  2. Bhanu
    Replies:
    2
    Views:
    1,341
    Roedy Green
    May 5, 2006
  3. rbt
    Replies:
    5
    Views:
    8,972
  4. David Pratt
    Replies:
    11
    Views:
    846
    Dennis Lee Bieber
    Mar 9, 2006
  5. Chris  Chiasson
    Replies:
    6
    Views:
    612
    Richard Tobin
    Nov 14, 2006
Loading...

Share This Page