On Wednesday 12 December 2001 5:37 pm, Andre Poenitz wrote:
> On Wed, Dec 12, 2001 at 03:50:46PM +0000, Angus Leeming wrote:
> > Sorry for the off-topic question but:
> > 
> > What is the correct way to empty a stringstream to use it again? 
Currently, 
> > I'm using ss.str(string()) but I'm sure there must be a better way...
> 
> Don't do that. Use a new stream.
> I'll never know what flags have been changed.
> 
> Andre'

Thanks, André, I'll do that.

Here's a new question for you:

Is there a quick and easy way to go to a particular line in an ifstream?

Eg, grep -n "Some string" data_file
gives me a list of lines containing "Some string". It'd be nice to jump to 
each line, but istream::seekg(pos) goes to the char at pos, not to a line.

At the moment I have

int setline(ifstream & in, int desired_line_no)
{
        in.seekg(0, ios::beg);
        int line_no = 0;

        int start_line = -1;

        string buffer;
        int tellg = 0;
        for (;;) {
                if (line_no == desired_line_no) {
                        // success !
                        start_line = tellg;
                        break;
                }

                tellg = in.tellg();
                getline(in, buffer);
                if (!in.good())
                        break;

                ++line_no;
        }

        return start_line;
}


which takes ages to go to the end of a big file because I read each line into 
a string.

Alternatively, can you point me to a good streams reference. I've been unable 
to find anything relevant in Stroustrup.

Angus

Reply via email to