看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) win10 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) vc++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) none 問題(Question): 用visual studio讀檔時跑出亂碼 https://i.imgur.com/GpXTefB.jpg 但是在dev c那邊打一樣的程式卻沒事 讀檔程式碼如下圖 https://i.imgur.com/1dQDIS4.jpg 請各位幫我看看是什麼問題 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.215.245.250 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1505908303.A.E94.html
ilikekotomi: 輸出會到buf有0為止 加上buf沒初始化就可能超出範圍 09/20 20:11
ilikekotomi: 還有可能獨入檔案超過5000byte把buffer塞滿 09/20 20:12
ilikekotomi: 要考慮一下邊界條件的情況如何處理 09/20 20:13
eye5002003: int i=file.read(buffer,5000);if(i<5000)buffer[i]=0 09/20 20:27
eye5002003: read()會回傳究竟讀到多少資料,你要幫buffer下個結尾 09/20 20:30
asd456fgh778: Genuis 支援 09/21 10:01
hunandy14: 去查一下read的用法,第二個參數是幹嘛的 09/26 02:56