笔者这半年来一直是自己手动将编码后的数据一个一个敲到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();
}