In my development com there's no issue of handling 4MB of text file data but it took about 1-2mins for my program to finish.

I got a few questions here:
Q1-> how much can this string "line" store?
Q2-> should i use sstrm.clear() and line.clear() inside the while loop or only the sstrm.clear() inside the while loop then use line.clear() out of the while loop?
Q3-> do i really need to use .clear() on both the string for line and sstrm?
Q4-> or should i use other type of "get" to handle this 4MB text file?

Thanks in advance.

string line;
string match("sample");
while (getline(4MBfile, line))
stringstream sstrm(line);
string::iterator iter =search (

1) I'm not sure either, but probably the limit of an integer. See limits.h for that value.

2) you don't need to use clear() at all. getline() will do that for you unless it fails, such as at end-of-file. And you don't need to use clear() on sstrm either because it is destroyed and recreated on each loop iteration.

3) see #2

4) getline() doesn't read 4mg file all at one time, but only until it encounters '\n' (or in the case of MS-Windows "\r\n" pair), which is more than likely less than 100 or so bytes. get() will read just one byte at a time, which means you will have to write your own version of getline().

Springboarding off AD's comments, yes, the string type can hold a really large string. However, reading and storing long string data to it could be time consuming. String allocates a small data area at first, and keeps adding to that as needed. It may be the case that every time more storage is needed, it gets allocated and the current content copied, with new content stored after that.

How long are the actual lines within your 4MB file? Many short ones (so there's lots of I/O activity) or a few really long ones (leading to memory reallocations)?

You could initially set line's size to be the maximum you expect, then it won't have to deal with memory issues during the run.

string( size_type length, const char& ch );  //constructor with initial size
void resize( size_type size, const TYPE& val = TYPE() ); //set size of existing string

Thanks AD for the answers.

hi vmanes,

My file contained mostly short lines of max 70 chars.
but file content will be changing so after each changes my program will read into it.

which mean i didnt need to close my program, so is it advisable to use line.clear() just outside this while loop? or dont need as AD mentioned getline will do it for me?

As AD said, you don't need the .clear( ) except to restore the input stream to a good state after an error. That function does not "empty" the string, and there's no need to do such.

Since your lines are small, in a 4MB file your program is doing over 50,000 read actions. This accounts for some of the time overhead you're encountering. Do you also do screen display for each line? That would add even more time.

This article has been dead for over six months. Start a new discussion instead.