Re: Standard C++ file size ???
On Jul 7, 4:12 pm, "Leigh Johnston" <le...@i42.co.uk> wrote:
"Peter Olcott" <NoS...@OCR4Screen.com> wrote in message
news:WISdnUSOBfdgC6nRnZ2dnUVZ_hqdnZ2d@giganews.com...
On 7/6/2010 1:26 PM, James Kanze wrote:
On Jul 6, 3:47 pm, "Peter Olcott"<NoS...@OCR4Screen.com> wrote:
http://groups.google.com/group/comp.lang.c++/msg/d27c88e57faadeb5?hl=en
Will the method specified in the above link always work
correctly for static binary files. (By static I mean that
they will not be written to while they are being read).
It's not guaranteed by the standard, and there have been
systems where it wouldn't work. As long as the file is
opened in binary mode, and doesn't change, it will probably
work with most implementations under Windows or Unix (where
the system does maintain file position as a byte count,
rather than some other representation). There are almost
certainly mainframes and legacy systems, however, where it
won't work.
I adapted your code to my style making minor formatting
changes and tested it on several files. It worked perfectly.
What I liked best about your code is that it taught me
a very simple way to use exceptions.
I had previously avoided exceptions because their typical
implementation was far too convoluted, and I always strive
for the simplest possible code. Now that I know how to throw
and catch text based exceptions they are very simple. That
was the cleanest error handling that I have ever seen. This
will become my new idiom for file processing and error
handling.
Throwing string literals is a retarded bad habit, as is
throwing std::string objects, as you cannot differentiate
between different types of exceptions in catch blocks. Throw
a unique type (preferably derived from something from
<stdexcept>) instead. std::exception has a virtual what()
member function which you can override if you want your
exception objects to have some pretty string that you can
output.
Very much so. I would like to insist that in the posted code,
I threw string literals as a placeholder; presumably any actual
application would have its own exception class which it would
use. It never occured to me that someone might take that aspect
of the code literally. If I had it to do again, I'd throw
std::ios::failure instead, so the intent would be clearer.
In practice, there is only one special case where you might
throw anything other than a class type derived from
std::exception: if you want to terminate the program, and the
application framework supports it, you might throw an int
instead of calling exit. (But you'd never do this in a library,
since you don't know that the application framework will be
prepared for it.)
--
James Kanze