Always check whether I/O operations were successful
#Always check whether I/O operations were successful CodeCode like this is problematic:
FILE *fp = fopen(filename, "w");
fprintf(fp,"X,Y,Z\n");
If we failed to open the file we'll get a NULL pointer assigned to fp
. If we're really unlucky, the program will survive passing that to fprintf()
, and we'll have no indication that the data were not saved. That can be a serious issue if it means that the user believes their data have been safely stored.
Similarly, we should check the return value of sscanf()
when used on the arguments, before we attempt to use the values we read. (Alternative - consider strtoul()
or strtoul()
, so we can validate that there's no junk following a valid number).
#Always check whether I/O operations were successful Code like this is problematic:
FILE *fp = fopen(filename, "w");
fprintf(fp,"X,Y,Z\n");
If we failed to open the file we'll get a NULL pointer assigned to fp
. If we're really unlucky, the program will survive passing that to fprintf()
, and we'll have no indication that the data were not saved. That can be a serious issue if it means that the user believes their data have been safely stored.
Similarly, we should check the return value of sscanf()
when used on the arguments, before we attempt to use the values we read. (Alternative - consider strtoul()
or strtoul()
, so we can validate that there's no junk following a valid number).
Always check whether I/O operations were successful
Code like this is problematic:
FILE *fp = fopen(filename, "w");
fprintf(fp,"X,Y,Z\n");
If we failed to open the file we'll get a NULL pointer assigned to fp
. If we're really unlucky, the program will survive passing that to fprintf()
, and we'll have no indication that the data were not saved. That can be a serious issue if it means that the user believes their data have been safely stored.
Similarly, we should check the return value of sscanf()
when used on the arguments, before we attempt to use the values we read. (Alternative - consider strtoul()
or strtoul()
, so we can validate that there's no junk following a valid number).
#Always check whether I/O operations were successful Code like this is problematic:
FILE *fp = fopen(filename, "w");
fprintf(fp,"X,Y,Z\n");
If we failed to open the file we'll get a NULL pointer assigned to fp
. If we're really unlucky, the program will survive passing that to fprintf()
, and we'll have no indication that the data were not saved. That can be a serious issue if it means that the user believes their data have been safely stored.
Similarly, we should check the return value of sscanf()
when used on the arguments, before we attempt to use the values we read. (Alternative - consider strtoul()
or strtoul()
, so we can validate that there's no junk following a valid number).