C++ Lesson: Don't Mix >> and getline()

Language butchery by Mr Rich on  14.3.10 @ 12:13

Compounded logic problems in code are a pain in the arse. Consider the following:


int m_numEntries; // stores the number of entries in the data file
char tempLine[INPUT_LINE_MAX_LEN] = {NIL}; // templine
ifstream dataFile(filename, ios::in ); // the data file
...

dataFile >> m_numEntries; // first line in the file is Number entries
dataFile.getline(tempLine, 256); // get the first line



What was weird was that the read in line wasn't reading in. However, changing to the following got everything to read in correctly:


/* variables the same */
....
// dataFile >> m_numEntries; // < that didn't work
dataFile.getline(tempLine, INPUT_LINE_MAX_LEN);
m_numEntries = atoi(tempLine);
dataFile.getline(tempLine, 256); // get the first line



So now I know not to mix the stream input operator and getline()... But why?

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?