Re: Handling large text streams of integers
On Mar 31, 7:14 pm, Comp1...@yahoo.co.uk wrote:
Suppose infile is an ifstream bound to a text file containing
integers. The operation infile >> x1; reads integers into the
stream. How many times can this operation be done before
there are possible problems?
As many times as there are numbers in the file.
I know this question isn't exactly platform independent but
are there guidelines that can be given for writing portable
code?
The real guideline is the check the results after every
extraction.
How does the number of such operations (before using another
stream) compare with
std::numeric_limits<std::streamsize>::max() ?
A lot of systems maintain the current position in the file in an
std::streamsize. Which means that
std::numeric_limits<std::streamsize>::max() is also the maximum
number of bytes in the file. And that there can't be that many
numbers, since each number requires at least two bytes (one
digit and a separator).
But it's not something one worries about. A lot of files will
be smaller. Just check the status after the read (and before
using the value), and everything will be OK.
Does the above constant denote the max number of integers that
can be read into the stream? Or is the bound in terms of the
number of characters? For example, should the bound be halved
if every integer has exactly two digits?
That number may be (probably is) the maximum number of bytes you
can read from a file in a C++ program. However, it's normally
irrelevant, since most systems do not require that all files
have the maximum length. The number of values you can read is
limited by the number of values in the file.
Note that if, instead of reading a file, you are reading from a
device that doesn't support seeking (like a pipe, a keyboard or
a socket), then the position is irrelevant, and you can easily
read more.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34