【问题标题】:Reading from files and comparing/ merging data to create a new file从文件中读取并比较/合并数据以创建新文件
【发布时间】:2017-06-13 05:12:45
【问题描述】:

我真的很想得到一些帮助来解决这个问题。 我有两个文件,一个 txt 文件和一个 xls 文件,在 txt 文件中它包含学生姓名及其 ID 的列表。在 xls 文件中,它包含字母和分配给它们的一些数字。 我需要为每个学生创建一个新的 txt 文件,以他们的 ID 作为文件名。在这个文件中,我需要显示分配给组成学生姓名的字母表的数字。请帮帮我,在txt filexls file给定的时间内实在搞不定

到目前为止,我使用 C++ 能够做的是从 txt 文件中读取数据,并将 xls 文件转换为 txt 文件并从中读取。我需要将数据存储在数组中还是更容易转换为结果文件

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main()
{
    ifstream inputa("Students.txt");
    ifstream inputb("Alphabet.txt");
    if (!inputb.is_open()) std::cout << "Error: File Not Open" << '\n';


/* ofstream output;
output.open("output.txt");
 This was just to see if it was getting anything 
*/

string name;
string id;
string alphabet;
string number;


while (inputa >> name >> id || inputb >> alphabet >> number) { 
    /* here i planned to compare the strings using string::find, but it doesnt work */

    }







system("pause");

return 0;

}

【问题讨论】:

  • 如果您希望得到高质量的答案,您需要提供一个简单的示例和一些不起作用的代码。由于您已经弄清楚如何转换 xls 文件,请给出两个文件的内联示例、预期的输出文件以及您的代码不起作用或不完整。
  • 感谢回复,我已经添加了
  • 您还应该包含实际加载的转换后的 xls 文件(“Alphabet.txt”)。如果没有实际输入,就不可能给你准确的答案。乍一看,您应该首先将带有分配数字的整个字母表读入某种集合(可能是地图?)
  • 感谢Kirsz的回复,我现在也添加了

标签: c++


【解决方案1】:

在撰写本文时,您还没有给出包含 ID 和学生姓名的文件的示例,所以我可能弄错了顺序,但您应该明白了。

我会读入字母表文件,存储编码,然后一步写出所有学生文件:

#include <cstdlib>
#include <fstream>

static int encoding_map[256] = {0};

bool load_encoding(const char* file_name)
{
    std::ifstream file;
    file.open(file_name);
    if (!file.is_open()) return false;

    char from = 0;
    int to = 0;
    while (file >> from >> to) encoding_map[(int)from] = to;

    return true;
}

bool write_student_files(const char* src_file)
{
    std::ifstream file;
    file.open(src_file);
    if (!file.is_open()) return false;

    std::string id;
    std::string name;

    std::ofstream outfile;
    while (file >> id >> name)
    {
        outfile.open(id + ".txt");
        for (char c : name) outfile << encoding_map[(int)c];
        outfile << '\n';
        outfile.close();
    }

    return true;
}

int main()
{
    load_encoding("encoding.txt");
    write_student_files("students.txt");
    return EXIT_SUCCESS;
}

这是我的编码文件(字母文件),encoding.txt

a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
k 11
l 12
m 13
n 14
o 15
p 16
q 17
r 18
s 19
t 20
u 21
v 22
w 23
x 24
y 25
z 26

这是我的学生档案,students.txt

0 bob
1 susan
2 joe
3 becky

如果我用g++ -std=c++11 main.cpp 编译代码并运行它,我会在同一目录中获得 4 个新文件:

0.txt
1.txt
2.txt
3.txt

0.txt的内容例如是:

2152

如果你愿意,你可以弄乱空格,但是,使用我的简单编码:

b => 2
o => 15
b => 2

表示输出是正确的(虽然不是很漂亮)。

显然,这并没有做太多的错误处理,不处理包含多个单词的名称等,但你应该能够从这里弄清楚。

【讨论】:

  • 感谢理性编码者,这对我了解更多关于 c++ 的方式非常清晰和有帮助。
  • 我还想问一件事,如果在学生文件中,学生的名字被更改为随机字符串和符号或像omega这样的拼音,而在编码文件中,字母都被更改了怎么办?也适用于单个符号和字符串,这种方法也可以吗?
  • 是的,但您需要使用支持 utf-8 的编码。这是 ascii 特定的。另外,如果它解决了您的问题,请不要忘记接受答案。
  • 非常感谢 :) 我还是 c++ 新手,您介意向我解释一下如何使用或切换到支持 utf-8 的编码吗?
  • 嗨,很抱歉打扰你,但如果可能的话,你能看看我关于这个的新问题吗,这里是link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 2020-10-17
相关资源
最近更新 更多