You are here:

C++/C++ getline ()


my question is concerning the getline() function, using either gcc 3.0.4 or visual c++ 6.0 compilers (they act in the same way as far as this question is concerned). i'm trying to store the contents of a file in an array of character pointers. each line of the file starts with a number, then a single space, then a string of characters. i want the "number" to be read in first. this number is to be the index value of the array of character pointers in which i will store the the "string of characters" from the same line (i.e. the rest of the line). both the number and the following "single space" is to be discarded while the following string of characters (in the same line) is to be stored as a single char* element in the array of character pointers.

now, the problem is that after having read in the number (using the two parameter version of the function getline() -- because the number of digits is fixed and predictable), when i call getline() again, nothing is read in. in fact, when i use peek() to find out what the next character is (after reading in the number but before trying to read in the rest of the line w/ another call to getline() ), a -1 is returned. furthermore, i've found that after the first getline() call (to get the number), the end of the file has been reached, and yet, eof() does not return true. this is a problem i've been having consistently with the getline() function in numerous projects i've done in the past (and have thus far resorted to making my own getLine() function each time). please relieve my great confusion.. surly it cannot be a flaw in the implementation of the getline() function..? so what am i doing wrong?

Daniel, Thank you for your question.

'getline' is a Standard Template Library function. You must pass as its second argument a reference to an STL sequence. Furthermore, the 3-arg form of getline may function differently under different OSs since different character sequences may be used to end each line, or characters may be of different lengths.

In this example, note that it makes no sense to "store the contents of a file in an array of character pointers". Behind your back STL is doing lots of things: allocating and deallocating memory, updating pointers, and reading in pieces of files.

I avoid STL because it is so difficult to figure out what it is really doing.

If you really want to understand STL, I recommend that you read a book about it. An online guide is available at .

David Spector


All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2017 All rights reserved.