mariuszp wrote:
OK one of the tests failed and I'm a bit confused as to why:
Code:
rewind(f);
test_assert(fgetc(f) == 0x21);
test_assert(ftell(f) == 1);
test_assert(ungetc('x', f) == 'x');
test_assert(ftell(f) == 0);
test_assert(fseek(f, 0, SEEK_CUR) == 0);
test_assert(ftell(f) == 0);
the standard says fseek() must reverse the effect of ungetc().
http://pubs.opengroup.org/onlinepubs/00 ... fseek.htmlafter the call to ungetc(), the file position is still 1 due to the previous fgetc(), but reported position is 0 as expected. but the fseek() to SEEK_CUR should set the reported position to 1, as it is 0 bytes away from the current file position, as far as I understand. And yet the last test (test_assert(ftell(f) == 0) ) fails. What is wrong?
I think you are probably correct. I based my implementation on the doc for fflush() which says
fflush() wrote:
[CX] [Option Start] For a stream open for reading, if the file is not already at EOF, and the file is one capable of seeking, the file offset of the underlying open file description shall be set to the file position of the stream, and any characters pushed back onto the stream by ungetc() or ungetwc() that have not subsequently been read from the stream shall be discarded (without further changing the file offset).[Option End]
IOW, fflush() is optionally different from fseek() in this detail. My fseek() calls fflush() so operates incorrectly.