Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get an error when closing a file?

Tags:

c++

file

go

When programming I often open and close files. I understand that when opening a file, it's important to check for errors to see if it did in fact open. I don't know of an instance though when closing a file would be an issue (except for maybe if opening the file did give an error and it went unchecked).

I'm currently programming in go, and under the os package the .Close() function can return an error if it needs to. I know in C++ this is possible as well.

So finally, my question is what circumstances would cause an error when trying to close a file?

like image 991
Jean Avatar asked Jun 22 '16 20:06

Jean


People also ask

What does closing a file do?

The act of closing the file (actually, the stream) ends the association; the transaction with the file system is terminated, and input/output may no longer be performed on the stream. The stream function close may be used to close a file; the functions described below may be used to open them.

What happens if you dont close a file?

If you write to a file without closing, the data won't make it to the target file. But after some surfing I got to know that Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.

Why it is important to close a file how it is closed?

Because files are limited resources managed by the operating system, making sure files are closed after use will protect against hard-to-debug issues like running out of file handles or experiencing corrupted data.

What is the syntax when closing a file?

Syntax : int fclose( FILE * stream ); Return valu e : Here fclose() function closes the file and returns zero on success, or EOF if there is an error in closing the file.


2 Answers

Yes. Closing a file can cause an error/fail. See for example the errors that close(3) can return - http://linux.die.net/man/3/close - stuff like filesystem corruption is one probable cause.

Often you can't really do much about it though other than log an error and know that your data is probably screwed (depending on the error) - you can rarely "fix it" except for maybe re-writing the data somewhere else if you still have it etc.

The language you are using (Go, C++, whatever) is irrelevant. If the filesystem is full, corrupt or someone has physically ripped out the harddrive, then your programming language just can't save you and close will fail and it is anybodys guess whether your data are safely stored somewhere or not.

like image 186
Jesper Juhl Avatar answered Oct 07 '22 05:10

Jesper Juhl


At OS level, things can also go wrong when closing files, especially if you have written to them (i.e. buffering issues, out of disk space, media removed, etc...).

The C++ fstream has a void close() function which doesn't return anything. The principle is that once you called close you can no longer use the stream whether there was an error or not. However when a closing error occurs, the fail bit is set, so that it can be detected, and the error code can be retrieved the usual way.

The C fclose() returns an int which shows if there was an error or not. If you want to know why it failed you have to get and analyze the errno error code. It's not as self-explaining as a void function, but here also, you can't use the FILE* anymore after the closing call, whether there was an error or not.

like image 45
Christophe Avatar answered Oct 07 '22 04:10

Christophe