Re: ifstream and reading way past the EOL

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



sklett wrote:
> I am parsing a text file, when I know that I have hit a certain section
> that indicates I no longer need to read and interpret the rest of the
> file, but simply store it to write out later I do this:
> <code>
> // read the rest of the file into a single buffer
> int position = m_inStream.tellg();

tellg() doesn't return an int.

> m_inStream.seekg (0, ios::end);
> int end = m_inStream.tellg();

tellg() doesn't return anything valid when you are at the end of the stream,
IIRC.

Also, you failed to check the returnvalue of your read() operations. Asking
for help without checking for errors... tsk, tsk, tsk ;)

Try something along these lines (check the proper use of the functions and
types):

std::deque<char> buffer;
char tmp[100];
unsigned amount;
while( amount = in.readsome( tmp, 100))
buffer.insert( buffer.end(), tmp, tmp+amount);

Alternatively, use std::getline() in combination with a list<string>.

Hmmm, maybe the easiest thing is to use the fact that ostream have an
overload for streabuffer-pointers:

// store rest of file in m_tailing_data
std::stringstream tmp;
tmp << m_inStream.rdbuf();
m_tailing_data = tmp.str();


Uli

.



Relevant Pages

  • Re: Newbie looking for some direction
    ... TMP, or too many points, is a concept much like duplication. ... It gets you thinking about backgammon and it doesn't overwhelm ... You're obviously a strong player. ... your opponent's 5 point to hit. ...
    (rec.games.backgammon)
  • Re: ifstream and reading way past the EOL
    ... What is the preferred method ... > buffer.insert, tmp, tmp+amount); ... function, I'll look into this some more, thanks for the code snip. ...
    (microsoft.public.vc.stl)
  • setw and setfill
    ... I'm trying to replace some inefficient code with some nice new code, ... have hit a problem. ... I can replace tmp with an overloaded ostream via ... And through security holes, blind them... ...
    (alt.comp.lang.learn.c-cpp)
  • Never mind
    ... fseek (tmp, 0, SEEK_SET); ... As soon as I hit the send button, ... I'll be taking a nap now. ... Prev by Date: ...
    (comp.lang.c)