編寫了一個自動從編碼log中提取資料的程式
阿新 • • 發佈:2018-11-14
筆者這半年來一直是自己手動將編碼後的資料一個一個敲到excel中的,真是笨的可以,今天終於下定決心寫個小程式。
首先感謝下面的博主:
https://blog.csdn.net/sruru/article/details/7911675 告訴了我怎麼在main函式傳入引數
https://blog.csdn.net/lioncv/article/details/43151325 實現了從string中提取數字的方法
https://zhidao.baidu.com/question/560022081.html 實現了怎麼從txt檔案中讀取需要的一行
下面給出程式碼
#include<iostream> #include<fstream> #include<stdio.h> #include<string> #include<iomanip> #include<vector> using namespace std; void extractFiguresFromStr2Vec(string str, vector<double> &vec);//function in which extract double numbers for string int countLines(const char *filename);//function in which count line numbers of a file void main(int argc, char*argv[]) { //printf("%s\n", argv[1]); const char *filename = argv[1]; string content, str1, str2; int n = countLines(filename); ifstream file; file.open(filename, ios::in); for (int i = 0; i < n; i++) { getline(file, content); if (i == n - 21) str1 = content;//string containing BD-rate, BD-psnr is stored in str1 for further extraction if (i == n - 1) str2 = content;//string containing coding time is stored in str2 for further extraction } file.clear(); file.close(); vector<double> vec1;//vector containing BD-rate, BD-psnr data vector<double> vec2;//vector containing coding time extractFiguresFromStr2Vec(str1, vec1); extractFiguresFromStr2Vec(str2, vec2); cout << vec1[1] << "\t" << vec1[2] << "\t" << vec1[3] << "\t" << vec1[4] << "\t" << vec2[0] << endl; } void extractFiguresFromStr2Vec(string str, vector<double> &vec){ const char *s = str.c_str(); const char *pstr; int i = 0, j = 0; int k; int ndigit = 0; pstr = &s[0]; for (i = 0; *(pstr + i) != '\0'; i++){ if ((*(pstr + i) >= '0') && (*(pstr + i) <= '9') || *(pstr + i) == '.') j++; else{ if (j > 0){ string str; for (k = j; k > 1; k--){ str.append(pstr + i - k); } vec.push_back(atof(str.c_str())); ndigit++; j = 0; } } } if (j > 0){ string str; for (k = j; k > 1; k--){ str.append(pstr + i - k); } vec.push_back(atof(str.c_str())); ndigit++; j = 0; } } int countLines(const char *filename) { int n = 0; string temp; ifstream file; file.open(filename, ios::in); if (file.fail()) { printf("open file \"%s\" error\n", filename); exit(2); } else { while (getline(file, temp)) { n++; } return n; } file.close(); }