Friday, January 16, 2009

Judicious use of getline() in C++

When reading text files in C++ I use the getline() function. There are two getline() functions. One is a method of ifstream class. The other is a standalone function. I think it is either in string.cpp or istream.cpp. I am too lazy to look it up now.

When you use ifstream.getline(), you have to specify the number of characters that you want to read. You have to make sure that you choose that number such that it is not less than the character count of the longest line in the file that you are trying to read. If it is less than that and you use "while (!ifstream.eof())", you run into an infinite loop! This is a big deal if you are writing a generic file manipulation library since you cannot know the maximum number of characters in a generic file.

With the standalone getline() there is no such problem.

I have written two functions that implement the above getline() functions:

The text file I am reading is:

The output is as follows:

As you see, in the second method the number of characters is not sufficient to read "hello". It interestingly reads "hell" which is the place the program goes at that point I guess(!). Once that happens, the rest of the file cannot be read. It doesn't stop either. If I hadn't put the "if (lineNumber>maxNbOfLines)" block into the second function, it would not be able to detect eof and there would be an infinite loop.

I don't exactly know the reason of this behaviour. To learn that I have to study details of getline() and eof()... and right now I am too lazy for that.

Lesson learnt: Use the standalone version of getline()

No comments: