![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| NFS file handle question | prathamesh | Linux | 0 | 06-01-2008 11:25 PM |
| How to handle multiple rows in a file | ksmbabu | Shell Programming and Scripting | 2 | 05-14-2008 09:44 PM |
| Stale NFS file handle | alphasahoo | UNIX and Linux Applications | 3 | 05-12-2008 09:31 AM |
| Stale NFS file handle | rein | UNIX for Advanced & Expert Users | 5 | 01-30-2005 09:30 AM |
| NFS file handle | kazimir | UNIX for Advanced & Expert Users | 2 | 07-24-2002 09:11 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
deleting a file name by its handle
All,
I am having three function 1.open 2.process 3.close. Open will open a file and process will process a file and close will close the file .. I can able to close the file by its filehandler.Is there is anyway that i can get the file name by filehandle and remove it after closing the file .Please help .... Thanks, Arun. |
| Forum Sponsor | ||
|
|
|
|||
|
there is no simple way to get a filename from a file descriptor (handle).
If the file in question is a temporary file look into man tmpfile - the file is automatically deleted on close. Otherwise, it is easier to have the shell script that passed the filename (to the C process) delete the file. As last suggestion - pass the filename around as an argument Code:
char global_filename[128]={0x0};
int openit(char *filename)
{
int filedesc=open(filename, O_RDONLY);
if(filesdesc < 0) {
perror("error opening file");
exit(EXIT_FAILURE);
}
return filedesc;
}
void process(int fd)
{
}
void closeit(int fd)
{
if(close(fd) <0) {
perror("error closing file");
exit(EXIT_FAILURE);
}
}
void deleteit(char *filename)
{
remove(filename);
}
int main(int argc, char *argv[])
{
int fd=openit(argv[1]);
process(fd);
closeit(fd);
deleteit(argv[1]);
return 0;
}
|
|
|||
|
Thanks all for your answer .
As i saw jim told in delete function it takes argument as filename. But i want to know whether it is possible to get the filehandle as argument and then by using it get the file name and delete the file. Since in design i am having the control of filehandle only . Please let me know. Thanks Again, Arun Kumar. |
|
|||
|
I don't think you'll be able to get the file name. As suggested above, you can get information about the file (with the fstat() function call), but that doesn't help you because it won't give you a name.
What it will give you, however, is the device and id information about where the file is stored. My guess is that this information should be sufficient to unlink the file from the file system, however, if you *really* need to do this you'll have to start looking for the source code to "unlink" for your system. Then you'll need to piece together an unlink that can take the data from a file handle rather than a file name, and this will certainly be non-portable. Regardless, methinks this is a tough nut to crack without having the file name available to you when you want to delete it. |
|||
| Google UNIX.COM |