2015-09-24 30 views
-2

我試圖用類似內容從文件中讀取字符串:最後文件的行讀取給出不同的結果

r FFFF 

r FF 

r FFFF 

這裏是我的代碼:

int main() 
{ int a; ifstream ifile; 
    ifile.open("hi.txt"); 
    while (!ifile.eof()) 
    { 
     ifile.getline(data, 100); // read a line from file 
     a = strlen(data); 
     cout<<'length"<<a; 
    } 
} 

我得到意想不到的輸出:length7length5length6

我在做什麼錯?

+1

做什麼你想要發生 –

+1

http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong –

回答

0

'\ n'(換行符)在data。在Windows中,您還可能有一個「\ n \ r」來代替(或「\ r \ n」我不記得了),在最後一行

你可能沒有它在文本文件中