LinuxQuestions.org
Review your favorite Linux distribution.
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 03-04-2009, 04:56 AM   #1
priyadarshan
Member
 
Registered: Feb 2009
Location: Ahmedabad, Gujarat, India
Posts: 197

Rep: Reputation: Disabled
Exclamation C Programing- Very basic question


This is very simple question whose answer I want in lucid language.....

There is fopen() function to open file in C
and
fclose() function to close the file.....

Now what I wanna know is why is it necessary to write fclose() after fopen()and file manipulation things get completed???

Suppose we dont use fclose() and if we open new file by fopen() than what the hell wrong will be there?

What does actuslly fclose() do then????
 
Old 03-04-2009, 09:30 AM   #2
tokico
Member
 
Registered: Jan 2009
Posts: 49

Rep: Reputation: 18
According to this, http://www.opengroup.org/onlinepubs/...sh/fclose.html, it closes the stream to the file, so I think that it will release memory. But not sure.
 
Old 03-04-2009, 10:24 AM   #3
akuthia
Member
 
Registered: Oct 2007
Location: triad, nc, usa
Distribution: Ubuntu
Posts: 232

Rep: Reputation: 29
i would say another (possible) reason would be to prevent corruption of data. If you close a parent file/program without having the other files closed out, i THINK damage CAN be done. And if nothing else, i think tokico is right. if the file is open (but not being used) it's just eating up resources.
 
Old 03-04-2009, 09:10 PM   #4
T74marcell
Member
 
Registered: Mar 2009
Posts: 102

Rep: Reputation: 18
You can fopen as much files as the limits.h header allows, which is a large number, and never use fclose. When your application quits, all open file streams are closed by the C library or the operating system anyway.

A problem rather occurs if you use the same variable name for each fopen (maybe in some nested loop), thereby overwriting your old file pointer with the new one, so that you loose access to those previously opened streams. Doesn't necessarily hurts your application, but it doesn't make sense and is a silly way of programming.

Using fclose is a means of keeping system resources in balance, not hitting the max number of allowed open streams, and to make sure that all changes are buffered back to the hard disk, though the last option depends on the filesystem as well. You can enforce writing the buffered data to disk with the fflush function.

Arch Linux

Last edited by T74marcell; 03-14-2009 at 12:49 AM.
 
Old 03-05-2009, 04:00 AM   #5
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by priyadarshan View Post

What does actuslly fclose() do then????
It closes the file. If the file is not closed, the file state is not necessarily consistent.

You may wonder why this is important (provided that you close the files sometime, I guess...). Well, if the system shuts down with unclosed files the data in them may or may not be lost and the fileystem may or may not be corrupted.

Deliberately corrupting your files or taking unnecessary risks of corrupting the whole filesystem are not generally considered to be good programming practice...
 
Old 03-05-2009, 06:02 AM   #6
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
When you write to a file, you're not actually writing to the disk; rather, you're writing to a buffer (memory set aside for buffering input and output). When the buffer fills up, then the disk write actually happens (although you can control this, most of the time you have nothing to do with it, it's done by the system). This small C program, sizes.c, will show you, among other things, the size in bytes of BUFSIZ on your system (on mine it's 8192, yours may differ); BUFSZ is the size of the system buffers. If your program stops for some reason or other (like, say, it crashes) and you have one or more files hanging open with buffer(s) that haven't been written to the disk, well, bad pizza. So the "right" way is open-write-close. The fclose() function flushes the buffer(s) to the disk, thus making sure that everything you "wrote" actually gets there; you can also, if you're doing a large number of writes over a long period of time, invoke the fflush() function periodically to flush the buffers, but you still want to fclose() when you're done (and, no, you don't need to fflush() every write -- that just causes excessive disk input and output and is not useful).

Maybe this is handy, maybe not, but here tis:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <values.h>

int     main    (void)
{
        (void) fprintf (stdout, "char\t\t%d\n", sizeof (char));
        (void) fprintf (stdout, "short\t\t%d\n", sizeof (short));
        (void) fprintf (stdout, "int\t\t%d\n", sizeof (int));
        (void) fprintf (stdout, "long\t\t%d\n", sizeof (long));
        (void) fprintf (stdout, "float\t\t%d\n", sizeof (float));
        (void) fprintf (stdout, "double\t\t%d\n", sizeof (double));
        (void) fprintf (stdout, "BUFSIZ\t\t%d\n", BUFSIZ);
        (void) fprintf (stdout, "FILENAME_MAX\t%d\n", FILENAME_MAX);
        (void) fprintf (stdout, "time_t\t\t%d\n", sizeof (time_t));
        (void) fprintf (stdout, "MAXSHORT\t%d\n", MAXSHORT);
        (void) fprintf (stdout, "MAXINT\t\t%d\n", MAXINT);
        (void) fprintf (stdout, "MAXLONG\t\t%ld\n", MAXLONG);
        exit (EXIT_SUCCESS);
}
Save this as, say, sizes.c, type make sizes and run it.

Hope this helps some.

Last edited by tronayne; 03-05-2009 at 06:05 AM.
 
  


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
Where would a good place to start learning basic programing and scripting? Game Pro Programming 10 03-04-2008 07:06 PM
Is there a Linux programing lang similar to visual basic vansch76 Linux - Software 2 12-27-2007 10:08 PM
Is there a program for programing in Linux similar to Visual Basic ?? Xeratul Linux - Software 3 07-12-2006 06:35 AM
Newbie programing question matthewa Programming 4 03-20-2005 04:37 PM
C programing question israel Programming 2 11-29-2004 06:05 AM

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

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