I get that you are trying to squeeze every last drop of performance out of your code so that you do well in the olympiad. I guess what others are saying is that in the real world, sometimes a few msecs of performance is not an issue. But the olympiad is not the real world.
Another thing you will learn in the real world is you need to use a style that is easier for others to read and understand. The following rewrites make your code a little bit easier to understand
without hurting performance.
original:
if (pos == BUF_SIZE) fread(buf, BUF_SIZE, 1, fin), pos = 0;
rewrite:
1 2 3 4 5
|
if (pos == BUF_SIZE)
{
fread(buf, BUF_SIZE, 1, fin);
pos = 0;
}
|
original:
while (!isdigit(ch = nextch()));
rewrite:
1 2 3 4 5
|
while (!isdigit(ch = nextch()))
{
/*no-op - just consume the character */
;
}
|
This one (semi-colon at the end of a while statement) is actually an example of a common coding mistake and should be flagged in a reasonably thorough code inspection. Expressly commenting the no-op explains to the casual reader that the behavior is intentional.
However, @helios stated the following:
One problem I see with this implementation is that there's no checking to see if the size of the input file is a multiple of 217. |
Regardless of whether the file is a multiple of 2
17, I don't see how you know that the file is exhausted. If you have read the entire file and continue to call read(), you will:
- continue to read past the last byte read during the last fread until pos = 2
17
- then, call fread (which will fail), reset pos = 0, and keep re-reading the bytes in the array.
There is nothing in your code to determine if your fread was successful and how many bytes were read (checking the return value from fread tells you that). And there is no way for read() to indicate that it did not generate another integer value.