R. Clayton (rclayton@clayton.cs.monmouth.edu)
(no date)
Is this code ok for finding the number of bytes in a file?
ofstream file;
fil.open("hello",ios::app);
int p = file.tellp(); //where p is no: bytes
No. tellp() and tellg() return a value of type pos_type, which is not required
to be an integer (it may be an integer, but it doesn't have to be). Assuming
the tell functions return an integer is non-standard and non-portable. For
example, assuming it compiles, this code may produce wrong answers on microsoft
systems.
or using ifstream, moving the file to eof and then using tellg()
Still not ok. The problem is not how you get to a file position, but what the
tell functions return at the file position.
the reason i din't use this implementaion in design test was because i
misunderstood the question. i thought you did not want us to count newlines
and that is why ignored newlines.
Not even close. Lines were defined to include the trailing newline, so you
can't ignore them if you want to find correct line sizes, and newlines are
characters in the file, so you can't ignore them if you want to get a correct
count of the characters in the file.
Admittedly, the definition of newlines is problematic (which is why I mentioned
microsoft systems above), but the exact definition of newlines didn't matter
for the design test.
This archive was generated by hypermail 2.0b3 on Mon Aug 13 2001 - 15:00:04 EDT