You have a semicolon at the end of your while
loop:
while (getline(afile, foo, '\n'));
// ^
This causes the extraction to be performed but only the when the loop ends does foo
get printed. The last extraction doesn't extract anything which is why foo
is empty, hence the empty output.