[問題] sscanf的奇怪問題@@?

看板C_and_CPP (C/C++)作者 (是貼心鬼>///<)時間15年前 (2011/04/29 09:57), 編輯推噓0(002)
留言2則, 1人參與, 最新討論串1/5 (看更多)
開發平台(Platform): Scientific linux 補上g++版本gcc version 4.1.2 額外使用到的函數庫(Library Used): 問題(Question): 我想使用sscanf來parse以下的資料, addText(43,0,"OX0OY6.72X0Y2",0.100000, 335.420000,347.900000, 1,3,0); addText(44,0,"OX0.66OY0X2Y0",0.100000, 354.560000,335.580000, 1,3,0); 因此我寫這樣 sscanf( sentence, " addText(%d,0,\"OX%lfOY%lfX%dY%d\",0.100000, %lf,%lf, 1,3, 0);\n", &layer,&next[0],&next[1],&repeat[0],&repeat[1],&index[0],&index[1]); 然後再直接印出 printf(" addText(%d,0,OX %f OY %f X %d Y %d,%lf,%lf);\n", layer,next[0],next[1],repeat[0],repeat[1],index[0],index[1]); 看結果是否正確 餵入的資料(Input): addText(43,0,"OX0OY6.72X0Y2",0.100000, 335.420000,347.900000, 1,3,0); addText(44,0,"OX0.66OY0X2Y0",0.100000, 354.560000,335.580000, 1,3,0); 預期的正確結果(Expected Output): addText(43,0,OX 0.000000 OY 6.720000 X 0 Y 2,335.420000,347.900000); addText(44,0,OX 0.660000 OY 0.000000 X 2 Y 0,354.560000,335.580000); 錯誤結果(Wrong Output): addText(43,0,OX 0.000000 OY 6.720000 X 0 Y 2,335.420000,347.900000);--正確 addText(44,0,OX 0.660000 OY 2.000000 X 0 Y 2,335.420000,347.900000);--錯誤 程式碼(Code):(請善用置底文網頁, 記得排版) double min[2], max[2], next[2], index[2]; int repeat[2]; sscanf( sentence, " addText(%d,0,\"OX%lfOY%lfX%dY%d\",0.100000, %lf,%lf, 1,3, 0);\n",&layer,&next[0],&next[1],&repeat[0],&repeat[1],&index[0],&index[1]); printf(" addText(%d,0,OX %f OY %f X %d Y %d,%lf,%lf);\n", layer,next[0],next[1],repeat[0],repeat[1],index[0],index[1]); 補充說明(Supplement): 其實是不只這一行錯誤,還有其他的資料sscanf也吃不進去,對這個錯誤百思不得其解, 因此想請教各位高手不知道是否有碰過類似問題,先謝謝各位看完!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.138.152.179 ※ 編輯: piness 來自: 140.138.152.179 (04/29 09:59)

04/29 12:47, , 1F
很好奇你怎麼不要濾掉 addText, OX 這類的資料, 基本
04/29 12:47, 1F

04/29 12:48, , 2F
上它和程式邏輯並無關聯, 沒道理特地 hard code 進去
04/29 12:48, 2F
文章代碼(AID): #1DkXiDHz (C_and_CPP)
文章代碼(AID): #1DkXiDHz (C_and_CPP)