3 ms·
In my opinion that's bad C/C++ code: it's C/C++ code, two `while (!feof(fd))` loops, compiled without enabled diagnostics, and an inferior parser with at least
by cremno 12y ago
In my opinion that's bad C/C++ code: it's C/C++ code, two `while (!feof(fd))` loops, compiled without enabled diagnostics, and an inferior parser with at least one possible integer overflow (in ConsumeInteger()).
If it works without problems for your data, good for you, but for general purpose it isn't nearly a replacement as fscanf() or strtod() (hopefully) work correctly and don't have weird limitations like 300 / -300 as max / min base 10 exponent. Parsing floating point numbers is not easy.
- unwind 12y agoIt's very bad. Using feof() to somehow ask if a stream has reached EOF before doing any I/O is wrong. Code like this comes up very often on Stack Overflow so it seems it's somehow intuitive for people, or suggested by some tutorials or something. The purpose of feof() is to answer, after I/O has failed, if it failed due to the stream reaching EOF. It's also utterly pointless in this code, it should just check the return value of fscanf() and stop when it fails to convert the desired number of values. This, on the other hand, seems deeply surprising to most people, you very rarely see code that cares about the return value when scanning. Which is strange; I/O can fail. Further, since fscanf() basically ignores embedded whitespace, I'm not at all sure it will do as expected and read six floats per physical line of input. Lastly, yes of course it's strange to use the super-flexible fscanf() if you're looking for performance.
- asveikau 12y agoIt's not bad C/C++ code because it doesn't compile as C. It's just bad C++.