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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
03-04-2009, 04:56 AM
|
#1
|
Member
Registered: Feb 2009
Location: Ahmedabad, Gujarat, India
Posts: 197
Rep:
|
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????
|
|
|
03-04-2009, 10:24 AM
|
#3
|
Member
Registered: Oct 2007
Location: triad, nc, usa
Distribution: Ubuntu
Posts: 232
Rep:
|
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.
|
|
|
03-04-2009, 09:10 PM
|
#4
|
Member
Registered: Mar 2009
Posts: 102
Rep:
|
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.
|
|
|
03-05-2009, 04:00 AM
|
#5
|
Senior Member
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070
|
Quote:
Originally Posted by priyadarshan
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...
|
|
|
03-05-2009, 06:02 AM
|
#6
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
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.
|
|
|
All times are GMT -5. The time now is 08:17 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|