看板 b92902xxx 關於我們 聯絡資訊
※ 引述《beagle1 (滿城飛絮風吹雪)》之銘言: : 所以改用 fgets 來取代 gets 幾乎已經成為慣例了... : 用法其實差不多, 以你的程式可以把 gets(line) != NULL 改成這樣: : fgets(line, SIZE*5, stdin) != NULL : 效果完全一樣, 只差在 input line 過長 (超過 size) 的時候會被截斷, : 避免 segmentation fault 之類的悲慘結果... : 此外... : 我用 fgets 之後, 還會習慣在最後一個 char 填 '\0'; : 因為呢, 萬一真的過長被截斷, 這個 char array 是沒有字串結束記號的, : 所以接下來如果用 strlen() 之類的 function 去處理, 有可能跑不完... : 所以動手處理字串之前我會再加這樣一行: : line[SIZE*5-1] = '\0'; : 好, 這樣一來這個 line buffer 應該就不會因為 input 的單行太長 : 而給你帶來麻煩了... $ man 3 fgets fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer. 所以不用擔心 把那個分號收起來好好利用吧 :) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.167.182.90 ※ 編輯: scwg 來自: 218.167.182.90 (11/20 20:38)
euphrate:了 推 61.230.37.1 11/20