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

作者: yyan1218 (今天下雨了嗎)   2014-12-05 22:17:00
※ 引述《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
看個人覺得哪個好用 就用哪個
作者: bibo9901 (function(){})()   2014-12-06 00:24:00
你的 column 和 row 跟一般的定義不太一樣 XD

Links booklink

Contact Us: admin [ a t ] ucptt.com