Re: getline() from string ?
"Leigh Johnston" <email@example.com> wrote in message
"Peter Olcott" <NoSpam@OCR4Screen.com> wrote in message
On 5/26/2010 9:00 AM, Victor Bazarov wrote:
On 5/26/2010 9:54 AM, Peter Olcott wrote:
I need to know the syntax for getting a line of text from a std::string
buffer. Is there a way to do this besides manually?
std::string FileBuffer; // Holds the entire contents of the file.
std::string LineOfInput; // Holds one "\n" delimited line of text.
You can define an istringstream based on your input (FileBuffer) and
then use 'getline' with that stream. Rinse and repeat.
I already know how to do this with a file. The problem is that this is at
least fifty-fold slower than necessary. I want to read the whole contents
of the file, and then do a getline() on this buffer.
Sounds like you are doing premature optimization again. If your file I/O
is buffered then I would not expect much difference performance wise
between using getline on an istringstream and using getline on an
ifstream, I certainly find a "fifty-fold" increase in performance hard to
believe. As you haven't implemented it yet (you are asking how to do it
here) how do know there is such an increase in performance?
1) Make it work
3) Make it fast
I did a quick test using a 3.5 MB file (100,000 lines) using the following:
timer t("ifstream: ");
timer t("istringstream: ");
std::ifstream::pos_type count = file.tellg();
ifstream: 0.2454 seconds
istringstream: 0.2432 seconds
which implies no real difference in performance between the two methods like
I predicted (certainly not a 50x performance difference). Also performing
getline on an ifstream directly has lower RAM requirements.