istreamでは、いくつかの文字を受け入れるためにcin.getline()を使用し、EOF信号を入力します(私のOSはMACで、Ctrl + Dキーを押します)、残りの部分を受け入れるためにcin.getlineストリームの しかし、最初のcin.getline()の前にcin.eof()の値を3回、最初のcin.getline()と2番目のcin.getline()の間でテストします。 このプログラムでは、私はすべてこの3つのcin.getline()を終了させるためにEOFシグナルを使います。C++ cin.eof()の値は何を表していますか?
コード:
#include<iostream>
using namespace std;
int main()
{
cout<<"now EOF:"<<cin.eof()<<endl;
char character[10];
cout<<"input ten digit character,with '!' in the middle,end with EOF(ctrl+d):"<<endl;
cin.getline(character, 10,'!');
cout<<endl;
cout<<"get the character:"<<endl;
cout<<character<<endl;
char character2[10];
cout<<"now EOF:"<<cin.eof()<<endl;
cout<<"press(ctrl+d):"<<endl;
cin.getline(character2, 10,'!');
//cin>>character2;
cout<<endl;
cout<<character2<<endl;
cout<<"now EOF:"<<cin.eof()<<endl;
}
結果はここにある:
now EOF:0
input ten digit character,with '!' in the middle,end with EOF(ctrl+d):
123!456
get the character:
123
now EOF:0
press(ctrl+d):
456
now EOF:1
が、私はコメント部分のCIN >> CHARACTER2でcin.getline( '!' CHARACTER2、10、)に置き換えたときに:
結果は次のとおりです。
now EOF:0
input ten digit character,with '!' in the middle,end with EOF(ctrl+d):
123!456
get the character:
123
now EOF:0
press(ctrl+d):
456
now EOF:0
私は、なぜこれが起こり、どのようにcin.eof()値が変化するのか知りたいです。 ありがとうございました!
2番目の例では、 "cin >>"はEOF信号を文字列として扱い、何もしないことを意味しましたか? – YOUNG