[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Remove() for directories

Quentin Pope

8/25/2011 4:30:00 PM

Hi I am trying to delete a directory portably, using remove(). However it
always returns EOF and doesn't delete the directory.

Is there another function I should be using?

Thanks
QP
16 Answers

robertwessel2@yahoo.com

8/25/2011 4:41:00 PM

0

On Thu, 25 Aug 2011 16:29:47 +0000 (UTC), Quentin Pope
<qp19433@hotmail.NOSPAM.com> wrote:

>Hi I am trying to delete a directory portably, using remove(). However it
>always returns EOF and doesn't delete the directory.
>
>Is there another function I should be using?


There are no standard library functions in C that deal with
directories. You'll have to use something system specific.

Joe Pfeiffer

8/25/2011 4:49:00 PM

0

Quentin Pope <qp19433@hotmail.NOSPAM.com> writes:

> Hi I am trying to delete a directory portably, using remove(). However it
> always returns EOF and doesn't delete the directory.
>
> Is there another function I should be using?

As somebody else has posted, this can't be done portably. It's an OS
question, not a C question.

In a Unix-derived OS, you want the rmdir() call.

Ben Pfaff

8/25/2011 4:50:00 PM

0

Quentin Pope <qp19433@hotmail.NOSPAM.com> writes:

> Hi I am trying to delete a directory portably, using remove(). However it
> always returns EOF and doesn't delete the directory.

ISO C doesn't say anything about directories, but POSIX says that
remove() will delete a directory. That's about as portable as
you're going to get.

Are you sure that the directory is empty?
--
Ben Pfaff
http://be...

Lew Pitcher

8/25/2011 4:50:00 PM

0

On August 25, 2011 12:29, in comp.lang.c, qp19433@hotmail.NOSPAM.com wrote:

> Hi I am trying to delete a directory portably, using remove().

remove() is not a portable function, and does not exist in ISO C. For that
matter, the concept of directories also does not exist in ISO C,

> However it always returns EOF

Not likely. You need to reread the documentation on that function.

For instance, if you use the POSIX remove() function, then it either returns
0 for success or -1 for failure. If it returns -1, then "errno is
set appropriately" for the error encountered.


> and doesn't delete the directory.
>
> Is there another function I should be using?

Perhaps. But then again, you probably should learn how to properly use the
function you first selected.

--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfr... | Just Linux: http://jus...
---------- Slackware - Because I know what I'm doing. ------


Joe Pfeiffer

8/25/2011 4:52:00 PM

0

Quentin Pope <qp19433@hotmail.NOSPAM.com> writes:

> Hi I am trying to delete a directory portably, using remove(). However it
> always returns EOF and doesn't delete the directory.
>
> Is there another function I should be using?

Taking a quick look at remove()... it's returning -1, which is of
course the same value as EOF, but isn't EOF. It also sets errno when an
error happens, so you ought to be able to use perror() to find out what
went wrong.

blp

8/25/2011 5:13:00 PM

0

Lew Pitcher <lpitcher@teksavvy.com> writes:

> On August 25, 2011 12:29, in comp.lang.c, qp19433@hotmail.NOSPAM.com wrote:
>
>> Hi I am trying to delete a directory portably, using remove().
>
> remove() is not a portable function, and does not exist in ISO C.

Sorry, you're wrong. From C99 (but it wasn't new in C99):

7.19.4.1 The remove function

Synopsis
1 #include <stdio.h>
int remove(const char *filename);

Description
2 The remove function causes the file whose name is the string
pointed to by filename to be no longer accessible by that
name. A subsequent attempt to open that file using that name
will fail, unless it is created anew. If the file is open,
the behavior of the remove function is
implementation-defined.

Returns
3 The remove function returns zero if the operation succeeds,
nonzero if it fails.


> For that matter, the concept of directories also does not exist
> in ISO C,

You're right about that.

>> However it always returns EOF
>
> Not likely. You need to reread the documentation on that function.
> For instance, if you use the POSIX remove() function, then it either returns
> 0 for success or -1 for failure. If it returns -1, then "errno is
> set appropriately" for the error encountered.

EOF is commonly -1, so returning EOF on error is actually a
fairly likely outcome.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

Ben Pfaff

8/25/2011 5:14:00 PM

0

Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:

> Quentin Pope <qp19433@hotmail.NOSPAM.com> writes:
>
>> Hi I am trying to delete a directory portably, using remove(). However it
>> always returns EOF and doesn't delete the directory.
>>
>> Is there another function I should be using?
>
> As somebody else has posted, this can't be done portably. It's an OS
> question, not a C question.
>
> In a Unix-derived OS, you want the rmdir() call.

In a POSIX OS, remove() will remove a directory, just like
rmdir().

The Rationale even mentions UNIX in the description of remove():

7.19.4.1 The remove function

/usr/group provides the unlink system call to remove files.
The UNIX-specific definition of this function prompted the
C89 Committee to replace it with a portable function.

--
Ben Pfaff
http://be...

Nick

8/25/2011 7:17:00 PM

0

Quentin Pope <qp19433@hotmail.NOSPAM.com> writes:

> Hi I am trying to delete a directory portably, using remove(). However it
> always returns EOF and doesn't delete the directory.
>
> Is there another function I should be using?

What does errno (or perror for that matter) show the actual problem to
be?

Is the directory empty?
--
Online waterways route planner | http://ca...
Plan trips, see photos, check facilities | http://canalp...

Quentin Pope

8/25/2011 7:41:00 PM

0

On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:

> Quentin Pope <qp19433@hotmail.NOSPAM.com> writes:
>
>> Hi I am trying to delete a directory portably, using remove(). However
>> it always returns EOF and doesn't delete the directory.
>>
>> Is there another function I should be using?
>
> Taking a quick look at remove()... it's returning -1, which is of
> course the same value as EOF, but isn't EOF. It also sets errno when an
> error happens, so you ought to be able to use perror() to find out what
> went wrong.

Hi perror() says :-
remove: Is a directory

This would suggest to me that remove cannot act on directories. My system
is GNU/Linux.

Cheers
QP

Quentin Pope

8/25/2011 7:41:00 PM

0

On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:

> Quentin Pope <qp19433@hotmail.NOSPAM.com> writes:
>
>> Hi I am trying to delete a directory portably, using remove(). However
>> it always returns EOF and doesn't delete the directory.
>>
>> Is there another function I should be using?
>
> Taking a quick look at remove()... it's returning -1, which is of
> course the same value as EOF, but isn't EOF. It also sets errno when an
> error happens, so you ought to be able to use perror() to find out what
> went wrong.

Hi perror() says :-
remove: Is a directory

This would suggest to me that remove cannot act on directories. My system
is GNU/Linux.

Cheers
QP