[問題] 從 txt 檔讀浮點數存進陣列裡

看板C_and_CPP (C/C++)作者 (小耿)時間12年前 (2013/12/02 12:46), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Code blocks; linux 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 我要把一個測試的檔案讀進程式中, 用整數陣列測試時(有改陣列為int 與 輸出為%d)結果是正確的, 但用浮點數陣列測試時結果卻是錯的(偏偏我卻時要運算的是浮點數陣列 Orz) 餵入的資料(Input): 1.71111 1.11111 1 1.77778 1.11212 2 0 1 1.32542 1.02987 1.11123 3 0.05651 0.68765 1.34762 2 1.98865 0.46432 2 0.31926 預期的正確結果(Expected Output): 1.71111 1.11111 1 1.77778 1.11212 2 0 1 1.32542 1.02987 1.11123 3 0.05651 0.68765 1.34762 2 1.98865 0.46432 2 0.31926 錯誤結果(Wrong Output): 8.93447e-308 1.10874e-312 5.12181e+303 6.60419e-311 3.47555e-308 3.47639e-308 3.70979e-037 3.01513e+268 5.32571e-315 9.17825e-313 5.12181e+303 3.47619e-308 8.06589e-308 8.14853e-308 5.30499e-315 8.73947e+212 8.86025e+212 8.93456e-308 3.47625e-308 5.30499e-315 程式碼(Code):(請善用置底文網頁, 記得排版) int main() { int i,j; double image[5][4]; char *filename; FILE *fp; filename="test2.txt"; fp=fopen(filename,"r"); for(i=0;i<5;i++) { for(j=0;j<4;j++) { fscanf(fp,"%g",&image[i][j]); printf("%g\t",image[i][j]); } printf(" \n"); } 補充說明(Supplement): --   流光語翼編               | 流光幻現,訴說著過去的羽翼傳說似的 飛 翔 | 流光幻現,讚嘆著未來的羽翼不可預期的意象 | _____________________________________________|___ http://www.wretch.cc/blog/pipips                         | -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.122.151.189

12/02 12:58, , 1F
阿 解決了 Orz 把 image改成float即可 >"<
12/02 12:58, 1F

12/03 00:41, , 2F
double 的話用 %lf
12/03 00:41, 2F
文章代碼(AID): #1Id12lNP (C_and_CPP)
文章代碼(AID): #1Id12lNP (C_and_CPP)