2

次のコードの ifstream::operator>> の動作について質問があります。

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main () {
    ifstream inFile("test.txt");
    string buffer;
    while (!inFile.eof()) {
        inFile >> buffer;
        cout << buffer << endl;
    }
    return 0;
}

たとえば、test.txt の最後の行が空でない場合、このコードは完全に機能します。

One two
Three four
Five six

ただし、test.txt が次のように記述されている場合:

One two
Three four
Five six
(empty line)

に 2 つのcout「6」文字列が表示されます。Windows の \r\n などの問題でしょうか?Microsoft VC++ 2010 を使用しています。

前もって感謝します。

4

2 に答える 2