Lines 15-16: You're reading the file until you reach EOF. When you attempt to read past the last record, the EOF state bit is set for the file. You position to the beginning of the file and start reading again (line 21). Those reads are going to fail because the EOF state bit is still set. Before line 17, you need to call infile.clear(); to clear the EOF state bit. http://www.cplusplus.com/reference/ios/ios/clear/