0

出于某种原因,如果最后一行包含的字符少于缓冲区,我的程序不会打印文本文件的最后一行

#include <iostream>
#include <iomanip>
#include <fstream>

int main()
{
    std::ifstream read("test.txt");
    char buffer[12];

    while(!read.eof())
    {
        read.getline(buffer,11);
        if(!read.eof())
            std::cout<<buffer<<'\n';
        read.clear();

    }
    read.close();

    return 0;
}
4

1 回答 1

3

You are printing the line out under condition that if(!read.eof()), but that condition will evaluate to false after the last line was read with getline().

于 2013-06-24T06:44:50.670 回答