LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-16-2012, 10:25 AM   #1
postiwala
LQ Newbie
 
Registered: Oct 2005
Posts: 18

Rep: Reputation: 0
Holding file pointer of deleted file


Hi All,

I have a code something like below.

char buf [1000];
int len = 500;
FILE* fp = fopen(filename,"rb");
remove(filename);
size_t nread = fread(buf,1,len,fp));

Will the code work? Will fread be able to read the data?
If yes, please explain.
On my system, I can see that sometimes fread returns 0 and sometimes it returns 500.

Thanks.
 
Old 02-16-2012, 11:11 AM   #2
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by postiwala View Post
Code:
char buf [1000];
int len = 500;
FILE* fp = fopen(filename,"rb");
remove(filename);
size_t nread = fread(buf,1,len,fp));
Will the code work?
yes and no, depending on what you expect. Well, I didn't try, but in theory, the OS should refuse to delete an open file, and so remove() should fail. But you don't even check for that error condition, you ignore the result of remove().

Quote:
Originally Posted by postiwala View Post
Will fread be able to read the data?
Yes. At the time you tried to delete the file, it was open. So the delete action was refused, and the file remains intact.

Quote:
Originally Posted by postiwala View Post
On my system, I can see that sometimes fread returns 0 and sometimes it returns 500.
That's interesting. As I said: In theory, ...
And you know: Only in theory, theory and practise are exactly the same. ;-)

[X] Doc CPU
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to recover a deleted file using the ext3 file system wdhpr Linux - Newbie 6 01-11-2011 10:04 PM
amanda not able to copy BAckup file from holding disk to Tapes xudonw1 Linux - Server 8 05-18-2010 06:25 AM
Deleted log file - how to get back via file descriptor and keep alive? prollocks Linux - General 1 05-29-2009 09:08 AM
Need help to recover Deleted file - (file was saved in doc) Pravab Ubuntu 2 08-17-2008 10:57 AM
Getting file path from FILE pointer in C barisdemiray Programming 3 10-22-2004 12:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration