Re: complexity for tellg()
"toton" <firstname.lastname@example.org> wrote in message
On Feb 20, 11:46 am, "Alf P. Steinbach" <a...@start.no> wrote:
I am reading a big file , and need to have a flag for current file
position so that I can store the positions for later direct access.
However it looks tellg is a very costly function ! But it's code says
it should just return the current buffer position , thus should be a
very low cost function.
int pos = in.tellg();
This code takes 0.58 sec in my computer, while if I uncomment the line
in.tellg() , it takes 120.8 sec (varies a little )
can anyone say the reason & the possible workout ?
I amusing MS Visual Studio 7.1 and the std library provided by visual
Most likely the cause is conversion of CRLF to LF, which you've
specified by (1) opening the file in text mode and (2) compiling with a
One cure could then be to open the file in binary mode, and handle
newlines as appropriate (or not).
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
Q: What is the most annoying thing on usenet and in e-mail?
There are enough bad things related to new line ...
seekg and tellg doesn't match when newline char is \n , and file is
opened in text mode.
That shouldn't be, if you're just using seekg to return to a place
earlier memorized by tellg.
For the unix file,
int pos = in.tellg();
std::getline(in,line);///This doesn't print .PEN_DOWN !
Now if I open it in binary mode, Then this problem is solved.
But it creates another set of problems,
for unix file now it is fine, but for windows file \r is attached at
the end of line, as newline char is \n. So I need to remove \r from
the line if it is present.
If you wrote the file in binary mode, the \r characters wouldn't
be appended in the first place. It is important that you read and
write consistently, at least if you don't want to deal with local
conventions for reading and writing text files.
I wonder, what will getline will return in case of a mac file where
newline terminator is \r only. Will it return the total file as single
If you write in text mode and read in binary mode, that could happen,
Is there any std api support to take care of all these things, and yet
to make seekg & tellg consistent ?
Yes, it's called the Standard C++ library, if you use it right.