Saturday, December 12, 2009

fclose

How many time I and others have done this:

int error = 0;
FILE *fp = fopen("blad", "w");
if (fp){
while (getStuff( data, &dataLen)){
if (fwrite( data, dataLen, 1, fp) != dataLen){
error = 1;
break;
}
}
fclose(fp);
}
else
error = 1;
return error

Note that no error checking is done on fclose! the code meticulously checks the write process but neglects the final file closure.

A piece of code like this from an old data collection service program (> 10 years ago) burned me recently. Our enormous disk array filled up (for reason too lame to discuss) and it was never noted by this program. It just saved data to out zero bytes files! All the redundancies we put in place to prevent data loss failed because of this one piece of code.

No comments:

Post a Comment