When reading lines from files in a loop, the best place to put the getline is inside the while condition itself - this is the simplest way to make sure the file doesn't read over the end, and that the final iteration of the loop doesn't cause undefined behaviour
Code:
string str;
while( getline( infile, str ) )
{
// ... str was read from infile OK
}
Sometimes its necessary to put the getline elsewhere, maybe if the sequence of file reads is a more complicated process, you want to put checks at every point along the way make sure each step is successful
Code:
string str;
int i;
while( true )
{
infile >> i;
if ( !infile )
break;
getline( infile, str );
if ( ! infile )
break;
// read was successful - can do something with str and 'i'
}