例如有一个文本文件a.txt中有文本如下:
010001011030101011012016101011013022302040607140
020002022060202022022016101011013021401030502140
030003033090103033012017101011013025202030502070
040004041030204043022017101011013033102040603080
050005052060105051012018101011013011201030506130(一共一百行)
想要提取每行第35和36位存入b.txt,b.txt中文本格式如下:
22
21
25
33
11
然后根据另一文本文件c.txt中数据,c.txt中数据如下:
11 0.8
21 0.1
22 0.2
25 0.6
33 0.5
得到b.txt中每行数据在c.txt中所对应的小数,全部加和后赋值给a显示出来。
小白求代码!!!
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
int main(int argv, char *arg[])
{
fstream fA("a.txt");
ofstream fB("b.txt");
fstream fC("c.txt");
vector<string> words;
map<string, string> kv;
string k, v;
string line;
double a = 0.0;
string::size_type size;
while (getline(fA, line))
{
v = line.substr(34, 2);
words.push_back(v);
fB << v << endl;
}
fA.close();
fB.close();
while (getline(fC, line))
{
k = line.substr(0, 2);
v = line.substr(3);
kv[k] = v;
}
fC.close();
for (int i = 0; i < words.size(); i++)
{
a = a + stod(kv[words[i]]);
}
return 0;
}