Theory
Common mistakes
With files, a mistake usually shows not when compiling but when running – and often silently.
Common mistakes
- An unchecked
fopen. The file isn't there,f=NULL. On Windowsfgets(line, 64, f)quietly returnsNULLand the program acts as if the file were empty; on Linux it crashes. After everyfopen–if (f == NULL). "w"instead of"r".fopen(path, "w")empties the file at once, before anything is written. Open a file you read only with"r".- A forgotten
fclose. Part of the text stays in the buffer and, if the program crashes, never reaches the disk. For every successfulfopen– anfclose, and when writing, a checked one. while (!feof(f)). The last line is processed twice:feofbecomes true only after a failed read. The condition isfgets(...) != NULL.- An unchecked end of the number.
strtod("21,5", NULL)returns 21 and says nothing. Checkend. - A comma instead of a dot. Numbers in C use a dot. A file exported from a Lithuanian spreadsheet may have
21,5: that is an error in the file, and the program must report it, not guess. - The wrong folder.
No such file or directoryalthough the file is there: the program was started somewhere other than where its copy is.CMakeLists.txtlackscourse_data(...), or it was run from the terminal outsidebuild/debug. - An ignored error code.
read_measurements(...);withoutError e =– the code is thrown away, and the program works with an empty array.