Back in this thread http://www.cplusplus.com/forum/lounge/59766/ I suggested that OP try to write a file reversing utility. And then my conscience kicked in, so I took my own advice.
This is the C++11 source code for that thing.
I felt like my IQ dropped a little while writing it, so I'd like to know if anyone can spot bugs, design issues, or has suggestions for another (simpler) approach. Thank you.
Why do some people use the three-way string::compare() when they only need to check equality? I keep seeing this on the net here and there, and it seems odd.
@ Cubbi: Thanks. Honestly, I totally forgot about the == operator.
And last time I checked it wasn't a member of std::string, I guess that may answer your question.
Yes, in practise, it works on every implementation that I know of. So much so, that it is like assuming that a byte is an octet. Mentioned it because of the request for comments.
> Maybe this would be an issue for C89 only
If it is an issue with C89, it would also be an issue with C++11. C++ specifies the effect of seekoff() on a filebuf in terms of a call to std::fseek(), which is the C89 fseek() in the std namespace.
@JLBorges
That is an issue with the C99 standard, not the C90 or POSIX standards, which every extant implementation of seek() follows. C++, as you know, is C90 friendly, but C99 is antagonistic to it. I don't know why that language exists in the C99 standard, as it has been an industry standard practice to find a binary file's length that way, and it would impact existing code for a nominal worry over a mal-formed file.
> So fseek() shouldn't normally be trusted when used with SEEK_END?
In theory, fseek() used with SEEK_END should not be used on streams opened in binary mode.
In practise, haven't yet heard of an implementation where it does not work.
Note: fseek() used with SEEK_END is well defined for streams opened in text mode. Though a subsequent ftell() need not give the size of the stream. (Would not on Windows, Symbian, non-BSD versions of Mac OS etc.).