2013年12月19日 星期四

Visual Studio筆記 :: VC++讀取檔案與字串分割

開啟檔案: http://goo.gl/OwS1n
分割字串: http://msdn.microsoft.com/zh-tw/library/ftsafwz3(v=vs.90).aspx



char filename[]="data.raw";
fstream fp;
char line_buffer[128];

char seps[] = ",";
char *token1 = NULL;
char *next_token1 = NULL;

//==read data start==
fp.open(filename, ios::in);//開啟檔案
if(!fp){//如果開啟檔案失敗,fp為0;成功,fp為非0
cout<<"Fail to open file: "<<filename<<endl;
}
//cout<<"File Descriptor: "<<fp<<endl;

while(fp.getline(line_buffer, sizeof(line_buffer), '\n'))
{
//printf("%s\n",line);
j=0;
// use to split string
// reference: http://msdn.microsoft.com/zh-tw/library/ftsafwz3(v=vs.90).aspx
token1 = strtok_s( line_buffer, seps, &next_token1);
while (token1 != NULL)
{
data[i][j] = atof(token1);
printf ("i=%i j=%i %f\n",i,j,data[i][j]);
token1 = strtok_s( NULL, seps, &next_token1);
j++;
if (j==5) break;
}
i++;
}

fp.close();//關閉檔案
//==read data end==

原本用了另一個方法來分析字串(http://goo.gl/P9SZV), 但VC會出現安全性的Warning, 故採用MSDN的方法


data.raw內容:
0.97681,0.10723,0.64385,0.29556,1
0.67194,0.2418,0.83075,0.42741,1
0.20619,0.23321,0.81004,0.98691,1
0.51583,0.055814,0.92274,0.75797,1
0.70893,0.10836,0.33951,0.77058,1
0.55743,0.67804,0.061044,0.72689,1
0.15654,0.75584,0.01122,0.42598,-1
0.50462,0.15137,0.33878,0.41881,1
0.22657,0.59272,0.24103,0.46221,-1
0.49174,0.65115,0.24622,0.24796,-1
0.59512,0.26994,0.74692,0.32784,1
0.32439,0.37294,0.11623,0.94499,1

沒有留言:

張貼留言

Thanks for your message.

Python notes: Calculate delay time by WinDBG log

用WinDBG開Event Timestamps可以產生下面格式的log: Fri Sep 21 18:43:50.946 2018 (UTC + 8:00): @#$#^$@#$^ 以下python code用來找出兩個指定log中的時間差