Why are these two similar functions provided? (unget & putback)
I'm working with a file format where the type of record to read in next
is
stored in the first two bytes of the record. From a design standpoint,
I
wanted the class general enough to handle longer/more complex keys, so I
thought I should store this key value in the record class itself, and
query
the record types by a "canRead(std::i stream&)" function. This would
seem to
require an extended peek capability, since if the record type cannot
read
the stream, it must return the stream to its previous location.
I initially tried using tellg and seekg for this, and was rewarded by
60% of
the program being spent within these functions. Replacing these by
"unget"ing the proper number of characters reduced this to less than 1%.
All I know that unget() is much better on my PC, with my compiler than
seekg. Is this likely to be true in general for relatively small (<100)
numbers of bytes? When would I want to use putback(char) instead?
The only explanation I can think of is the file may be buffered somehow;
then ungetting might take you past the beginning of the buffer, whereas
putback'ing will be able to expand the buffer in this case.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
I'm working with a file format where the type of record to read in next
is
stored in the first two bytes of the record. From a design standpoint,
I
wanted the class general enough to handle longer/more complex keys, so I
thought I should store this key value in the record class itself, and
query
the record types by a "canRead(std::i stream&)" function. This would
seem to
require an extended peek capability, since if the record type cannot
read
the stream, it must return the stream to its previous location.
I initially tried using tellg and seekg for this, and was rewarded by
60% of
the program being spent within these functions. Replacing these by
"unget"ing the proper number of characters reduced this to less than 1%.
All I know that unget() is much better on my PC, with my compiler than
seekg. Is this likely to be true in general for relatively small (<100)
numbers of bytes? When would I want to use putback(char) instead?
The only explanation I can think of is the file may be buffered somehow;
then ungetting might take you past the beginning of the buffer, whereas
putback'ing will be able to expand the buffer in this case.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Comment