Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - Deleting files at a low level

Node:Deleting files at a low level, Next:, Previous:Finding file positions at a low level, Up:Low-level file routines



Deleting files at a low level

If you want to delete a file, you can use the low-level file routine unlink, as declared in the file unistd.h. Simply pass this routine the name of the file you wish to delete. If this is the only name the file has (that is, if no one has created a hard link to the file with the link function, the GNU command ln, or something similar), then the file itself will be deleted; otherwise, only that name will be deleted. (See the section "Hard Links" in the GNU C Library manual for more information on hard links.) If the file is open when unlink is called, unlink will wait for the file to be closed before it deletes it.

The unlink function returns 0 if the file or file name was successfully deleted. If there was an error, unlink returns -1. In addition to the usual file name errors, unlink can set errno to the following values. (See Usual file name errors, for a list of the usual file name errors.)


EACCES
Your program does not have permission to delete the file from the directory that contains it.
EBUSY
The file is currently being used by the system and cannot be deleted.
ENOENT
The file name to be deleted does not exist.
EPERM
Your program tried to delete a directory with unlink; this is not permitted under GNU. (See remove below.)
EROFS
The file name is on a read-only file system and cannot be deleted.

If you wish to delete a directory rather than an ordinary file, use the rmdir function. Simply pass it the name of an empty directory you wish to delete. It acts like unlink in most respects, except that it can return an extra error code in the system variable errno:


ENOTEMPTY
The directory was not empty, so cannot be deleted. This code is synonymous with EEXIST, but GNU always returns ENOTEMPTY.

 
 
  Published under free license. Design by Interspire