所以每次我使用while循環從文件中讀取字符串時,總會有一個額外的空字符串被最後處理。 ifstream fin(「A7infile.txt」);while(getline())循環在最後一次傳遞後沒有空字符串
while(getline(fin,line))
{
cout<<"Original Line: "<<line<<endl<<endl;
breakup(line,first,middle,last);
cout<<first<<" :is first"<<endl;
cout<<middle<<" :is middle"<<endl;
cout<<last<<" :is last\n"<<endl;
neww=makealpha(first,middle,last);
cout<<neww<<" :is the alphabetized line\n"<<endl;
}
fin.close();
return 0;
這是我用一個空字符串
Original Line: lolipops And Rainbows
lolipops :is first
And :is middle
Rainbows :is last
And Rainbows lolipops :is the alphabetized line
Original Line:
:is first
:is middle
:is last
:is the alphabetized line
我如何才能在最後一傳擺脫空字符串的意思嗎?
你可能有額外的換行符的地方,你可以只檢查'line.empty()'。 –
文件末尾是否有空行? –