看板 C_and_CPP 關於我們 聯絡資訊
請問以前用C讀取未知數量的資料時候 只要用FILE == EOF判定就好 可是現在改用C++則使用FILE.eof()來判定 但是我遇到一個問題 下列code要讀取未知數量的資料並且記錄數量 for (amount = 0; !infile.eof(); ++amount) { infile >> queue[amount].date1; infile >> queue[amount].date2; } amount -= 2; 但是這amount結果卻會因為檔案最後一行的空白行改變 如果最後一行沒有空白行的話amoun -= 1;才對 C語言的FILE == EOF則沒有這個問題 請問要怎麼解決? 因為要讀取的東西是int不是string 用getline還要切割很麻煩...... 先謝過前輩 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.114.221.33
chrisdar:int amount = 0 04/08 23:50
chrisdar:while (infile >> queue[amount].date1 >> queue[++amoun 04/08 23:50
chrisdar:amount -= 2; 04/08 23:50
chrisdar:推壞了 重推 04/08 23:51
chrisdar:1int amount = 0 04/08 23:52
chrisdar:while (infile >> queue[amount].date1 >> 04/08 23:52
chrisdar: queue[++amount].date2) {} 04/08 23:52
chrisdar:amount -= 2; 04/08 23:52
chrisdar:++amount 改 amount++ 04/08 23:55
fasthall:可以了 感謝!! 話說我還是不太懂++在前面和後面的差別... 04/09 00:08
gundan:單寫在一行沒有差異 a=b++; a=++b; 這種才有差 04/09 00:44
fasthall:可以稍微請前輩解釋一下嗎? 04/09 01:09
issuemylove:簡單說 ++b是在此行就改變值 而b++是在下一行 04/09 04:21
fasthall:所以第一次會先運算a等到運算完a才++? 04/09 09:47
gundan:a=b++;是 a=b;b=b+1; 然後a=++b;是 b=b+1;a=b; 04/09 09:54
fasthall:感謝解惑! 04/09 11:56