LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2012, 11:58 PM   #1
skaushal_lk
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu, CentOS
Posts: 27

Rep: Reputation: 4
[clarification] File handling fopen, open


Hi Friends,

I need a clarification that if I do fopen() then open() and use fclose() not using close() before fclose() will it cause issue?

Code:
main(){
        int fd = -1, ret;
        FILE *fp=NULL;

        fp = fopen("sk.txt", "r");

        if(NULL == fp)
        {
                printf("can't open file\n");
                exit(1);
        }

         if ((fd = open( "sk.txt", O_RDWR | O_CREAT | O_SYNC | S_IRUSR )) < 0)
        {
                printf("can't open file twice fd=%d\n", fd);
                
        }
       
                printf(" open file twice fd=%d\n", fd);

        fclose(fp);
}
Do I need to have close() before fclose?
 
Old 03-27-2012, 09:29 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well you need firstly close files that are open. your code there tests if the file fopen'd but in the end fclose's it regardless. not neat.

And as for you original query, they are different file handles, so have nothing in common at all, so you should not be looking at them as a connected entity. your code doesn't close fd, end of story.
 
1 members found this post helpful.
Old 03-27-2012, 11:17 PM   #3
skaushal_lk
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu, CentOS
Posts: 27

Original Poster
Rep: Reputation: 4
Thanx Chris for clarification.

Quote:
Originally Posted by acid_kewpie View Post
And as for you original query, they are different file handles, so have nothing in common at all, so you should not be looking at them as a connected entity. your code doesn't close fd, end of story.
As per your comment I should have fclose() for fopen() and close() for open() since both file handles are different. Otherwise prg won't close the file neatly before exiting.
 
  


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
[SOLVED] PHP File open larger files or fopen files larger than 2gigs Nemus Linux - Server 6 02-18-2011 01:03 AM
fopen 2 gb file schneidz Programming 5 10-26-2007 10:24 AM
Tip: handling "cannot open shared object file..." sundialsvcs Linux - Newbie 1 03-06-2006 12:57 PM
fopen: No such file or directory CatBreath Linux - Software 4 03-27-2005 06:33 AM
How to use fopen or something to open unicode filename Mathan Programming 10 09-23-2004 03:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:36 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