Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good to use exit() instead of fcloseall() for closing multiple files?

Tags:

c

file

exit

manpage

The man page of exit says,

  1. All open stdio streams are flushed and closed. Files created by tmpfile are removed.
  2. Parameters of exit i.e EXIT_SUCCESS and EXIT_FAILURE are slightly more portable (to non-UNIX environments) than the use of 0 and some nonzero value like 1 or -1. In particular, VMS uses a different convention.

The man page of fcloseall says,

  1. The standard streams, stdin, stdout, and stderr are also closed.
  2. The fcloseall function does not lock the streams, so it is not thread-safe.

Many online tutorials say that deallocating "all" resources of a given type is a programming error. They should be deallocated individually by the code that owns them or not at all.

So is it good to use exit instead of fcloseall?

like image 947
S D Avatar asked Aug 12 '15 18:08

S D


People also ask

Does exit () close files in C?

C library function - exit() The C library function void exit(int status) terminates the calling process immediately. Any open file descriptors belonging to the process are closed and any children of the process are inherited by process 1, init, and the process parent is sent a SIGCHLD signal.

Are file descriptors closed on exit?

Save this answer. Show activity on this post. Yes, the file will be automatically closed when the process terminates, regardless of the reason for the process termination. This is documented in POSIX.

Which function is used to close all open files in C?

Description. fclose( fileID ) closes an open file. fclose('all') closes all open files. status = fclose(___) returns a status of 0 when the close operation is successful.

How do you close all files in C++?

In a case, if a C++ program terminates, then it automatically flushes out all the streams, releases all the allocated memory, and closes all the opened files. Therefore, it is a good alternative to use the close() function to close the file-related streams, and it is a member of ifsream, ofstream, and fstream objects.


2 Answers

Short answer: yes, exit() is better.

Long answer:

It depends how you design your program. It is generally better to have each component clean up after itself, removing any temporary files and flushing buffers - this would be especially common for a C++ program where object destructors can handle this.

In that case you would use neither exit() nor fcloseall() - you'd have your components clean up after themselves then simply return from main().

For a C program you might prefer to design it expecting that everything will be cleaned up automatically at exit by one of these functions. If you are considering using either, then exit() is a safer bet because it will do more: it will do what fcloseall() does, plus it will remove temporary files created with tmpfile().

In addition, fcloseall() is a GNU extension which might affect the portability of your program.

Perhaps more importantly, these functions serve different purposes.

The purpose of exit() is to end your program.

The purpose of fcloseall() is to flush & close file descriptors, but not to end your program. It's perhaps most useful when you want to ensure you have closed all files (including stdin/stdout/stderr) before calling exec to start another process, without leaking any file descriptors into the new process that it will not expect and perhaps never close.

like image 163
Ted Percival Avatar answered Sep 28 '22 12:09

Ted Percival


The fcloseall function is a GNU extension. Using it makes your program less portable.

The exit function closes and flushes all stdio streams -- but it also terminates your program, which fcloseall doesn't do, so it's hardly a substitute.

There's probably no point in calling fcloseall if the program will continue to execute, and if it's just about to terminate anyway, exit will do the same thing.

The standard streams stdin, stdout, and stderr generally don't need to be closed explicitly. It's perfectly all right to let the system close them for you when the program terminates. If you've opened any other streams, say by calling fopen, you should keep track of each such stream yourself, and close it explicitly when you're finished with it.

like image 22
Keith Thompson Avatar answered Sep 28 '22 11:09

Keith Thompson