[問題] fscanf讀入csv檔的問題

看板C_and_CPP作者 (ㄐㄍ)時間5年前 (2019/05/16 09:06), 5年前編輯推噓0(005)
留言5則, 3人參與, 4年前最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) Win10 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) Code::Block 16.01 C語言 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 我的csv檔內資料是 型號 x尺寸 y尺寸 z尺寸 如果利用fscanf讀入的第一個資料型態為%s 後面所有的資料都會變成第一個字串裡的資料 餵入的資料(Input): 型號,x,y,z F,100,100,50 #1,130,130,60 #4,172,133,60 G,135,100,60 #263,100,61,61 H,130,125,65 預期的正確結果(Expected Output): 型號、x、y、z都可以順利存入個別的變數中並輸出 F,100,100,50 #1,130,130,60 #4,172,133,60 G,135,100,60 #263,100,61,61 H,130,125,65 錯誤結果(Wrong Output): F,100,100,50,6422260,6422264,6422268 #1,130,130,60,6422260,6422264,6422268 #4,172,133,60,6422260,6422264,6422268 G,135,100,60,6422260,6422264,6422268 #263,100,61,61,6422260,6422264,6422268 H,130,125,65,6422260,6422264,6422268 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) int main(){ char BOX_name; int x_size, y_size, z_size; FILE *Box01 = fopen("001.csv", "r"); if(Box01 == NULL){ perror(" File open failed: "); exit(0); } else puts("....File opened...."); printf("OK!\n"); while(!feof(Box01)) { fscanf(Box01, " %s,%d,%d,%d", &BOX_name, &x_size, &y_size, &z_size); printf("%s,%d,%d,%d\n", &BOX_name, &x_size, &y_size, &z_size); } fclose(Box01); return 0; } 補充說明(Supplement): 另外不懂的地方是 為什麼連輸出printf內的變數前面也要加&呢? 如果沒有加&的話就不會輸出...... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 163.24.85.97 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1557968780.A.F21.html ※ 編輯: JiGo (163.24.85.97), 05/16/2019 09:08:25

05/16 09:20, 5年前 , 1F
'char'是不同的'char *'。'char'是字元,用來存1個 byt
05/16 09:20, 1F

05/16 09:20, 5年前 , 2F
e;'char *'是指向字元的指標,可以用來指向一個字元陣
05/16 09:20, 2F

05/16 09:20, 5年前 , 3F
列當字串用
05/16 09:20, 3F

05/16 19:47, 5年前 , 4F
你可能需要初學教材,找本書或是網路資源
05/16 19:47, 4F

06/15 12:03, 4年前 , 5F
買本書吧,基本概念先懂比較好喔
06/15 12:03, 5F
文章代碼(AID): #1StBUCyX (C_and_CPP)