无法解决这个问题....我有两个txt文件,一个是encoding1.txt,其中包含一组十六进制数和分配给它们的ID号,例如
61 1
C3A4 2
C990 4
C991 5
C991CC83 6
C992 7
CA8C 9
另一个文件source.txt是源文件,其中包含一堆组合十六进制和学生名称
CA8CC992 Jack
C991C3A4 Amy
C991CC83 Sam
61C991 Tom
我想为每个学生输出一个txt文件,其名称为文件名,文件内应该是他们的十六进制分配的数字。例如对于jack.txt,里面应该是
9
7
下面是我试过的代码生成所有测试文件,但在其jack.txt内只有9个,其他文件为空。什么是正确的方法,我如何得到正确的结果?
#include "stdafx.h"
#include <iostream>
#include <string>
//#include<map>
//#include<unordered_map>
#include <fstream>
#include<array>
//#include<vector>
#include <algorithm>
using namespace std;
int main()
{
std::ifstream file;
file.open("encoding1.txt");
if (file.is_open())
{
std::cout << "opened encoding1 file";
}
std::ifstream file2;
file2.open("source.txt");
if (file.is_open())
{
std::cout << "opened source file" << std::endl;
}
std::ofstream outfile;
const int hexencosize = 7;
std::string hexenco[hexencosize] = {};
int id[7] = {};
std::string hexsource;
string * p;
std::string name;
for (int i = 0; i < 6; i++)
while (file >> hexenco[i] >> id[i]) {
std::cout << "hexenco is " << hexenco[i] << std::endl;
std::cout << "id is " << id[i] << std::endl;
};
for (int o = 0; o < 6; o++)
while (file2 >> hexsource >> name) {
outfile.open(name + ".txt");
std::size_t found = hexsource.find(hexenco[o]);
if (found != std::string::npos)
p = std::find(hexenco, hexenco + hexencosize, hexenco[o]);
if (p >= hexenco + hexencosize) std::cout << "Not found" <<
std::endl;
else outfile << id[p - hexenco] << '\n';
outfile.close();
};
system("pause");
return 0;
};