Re: [問題] C++ 讀取txt檔後,取出需要的值後儲存

看板C_and_CPP (C/C++)作者 (今天下雨了嗎)時間11年前 (2014/12/05 22:17), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《lettuce (lettuce)》之銘言: : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : VC++ : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : 問題(Question): : 各位先進們好,小弟有一筆資料如下,需要跳過!後的字串與數字,只取最下面數值的部分 : 並儲存成陣列,請問該怎麼做?謝謝 : 餵入的資料(Input): : !File:abc : !I:123 : !V:456 : !R:0.123 : 0.123 0.456 0.789 0.124 0.125 : 0.321 0.123 0.213 0.312 0.456 : 0.123 0.454 0.577 0.231 0.213 : 預期的正確結果(Expected Output): : 0.123 0.456 0.789 0.124 0.125 : 0.321 0.123 0.213 0.312 0.456 : 0.123 0.454 0.577 0.231 0.213 : 錯誤結果(Wrong Output): : 程式碼(Code):(請善用置底文網頁, 記得排版) : 補充說明(Supplement): 小弟也是菜鳥 可能方法不是最好,還請其他大大賜教 #include "stdio.h" #include "stdlib.h" //system函數 #include "string.h" #define MAXCOL 3 #define MAXROW 5 void main() { char S1[1024]=""; double Array[MAXCOL][MAXROW]={0}; //陣列大小 int col=0,row=0; while(col<MAXCOL) { scanf(" %[^\r\n]",S1);//抓取一行 //前面空白很重要,沒有的話不能一直抓取 if(S1[0]=='!') continue;//忽略此行 else { //偵測空白鍵分割字串 row=0; char *s1=strtok(S1," "); while(row <MAXROW && s1) { Array[col][row]=atof(s1); row++; s1=strtok(NULL," "); } col++; } } //打印陣列 for(col=0;col<MAXCOL;col++) { for(row=0;row<MAXROW;row++) { printf("%.3f ",Array[col][row]); } printf("\r\n"); } system("pause"); } 以上應該可以印出你要的答案0.0 陣列資料何時要停止收集,我設定是陣列存滿後跳出。 如果不想要用scanf 可以用看看 c++ std:cout cin 看個人覺得哪個好用 就用哪個 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.166.128.161 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1417789026.A.6D3.html

12/06 00:24, , 1F
你的 column 和 row 跟一般的定義不太一樣 XD
12/06 00:24, 1F
文章代碼(AID): #1KWRvYRJ (C_and_CPP)
文章代碼(AID): #1KWRvYRJ (C_and_CPP)