#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#define MAX 65535
using namespace std;
string name[MAX];
string num[MAX];
int max_num;
//删除字符串中空格,制表符tab等无效字符
string Trim(string& str);
//得到excel中的名称与代码
void get_Message();
//代码替换汉字
void Chinese_to_num();
//汉字替换代码
void num_to_Chinese();
//代码替换字符 函数
bool FileStringReplace(ifstream &instream, ofstream &outstream);
//汉字替换代码 函数
bool num_FileStringReplace(ifstream &instream, ofstream &outstream);
string Trim(string& str)
{
//str.find_first_not_of(" \t\r\n"),在字符串str中从索引0开始,返回首次不匹配"\t\r\n"的位置
str.erase(0,str.find_first_not_of(" \t\r\n"));
str.erase(str.find_last_not_of(" \t\r\n") + 1);
return str;
}
int main()
{
//获取码表
get_Message();
//用码表替换 文本文件
//Chinese_to_num();
//num_to_Chinese();
return 0;
}
void get_Message()
{
ifstream fin("C:\\Users\\dell\\OneDrive\\桌面\\测试二\\码表.csv"); //打开文件流操作
string line;
int i=0;
while (getline(fin, line)) //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
{
istringstream sin(line); //将整行字符串line读入到字符串流istringstream中
vector<string> fields; //声明一个字符串向量
string field;
while (getline(sin, field, ',')) //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
{
fields.push_back(field); //将刚刚读取的字符串添加到向量fields中
}
name[i] = Trim(fields[0]); //清除掉向量fields中第一个元素的无效字符,并赋值给变量name
num[i] = Trim(fields[1]); //清除掉向量fields中第二个元素的无效字符,并赋值给变量age
i++;
}
max_num=i;
for(int j=0;j<i;j++)
cout<<name[j]<<"\t"<<num[j]<<endl;
ofstream ofs;
ofs.open("编码.txt",ios::out);
for(int j=0;j<i;j++)
ofs<<name[j]<<"\t"<<num[j]<<endl;
ofs.close();
//测试数据数量
cout<<max_num;
}
void Chinese_to_num()
{
//汉字化为代码
string file_path = "1.txt";
string out_path = "2.txt";
ifstream instream("1.txt"); // instream.open(file_path) 默认以ifstream::in打开
ofstream outstream("2.txt"); // outstream.open(out_path) 默认以ostream::out打开,文件内容会被丢弃,可使用app模式(写指针会被定位到文件末尾)
FileStringReplace(instream, outstream);
instream.close();
outstream.close();
}
bool FileStringReplace(ifstream &instream, ofstream &outstream)
{
string str;
size_t pos = 0;
int j=0;
while (getline(instream, str)) // 按行读取
{
for(int j=0;j<max_num;j++)
{
//先查找第一个名词是否存在
pos = str.find(name[j]);
//存在,代码替换
if (pos != string::npos)
str = str.replace(pos, name[j].size(), num[j]); // 代码替换汉字
}
outstream << str << endl;
}
return true;
}
//汉字替换代码 函数
bool num_FileStringReplace(ifstream &instream, ofstream &outstream)
{
string str;
size_t pos = 0;
int j=0;
while (getline(instream, str)) // 按行读取
{
for(int j=0;j<max_num;j++)
{
//先查找第一个名词是否存在
pos = str.find(num[j]);
//存在,代码替换
if (pos != string::npos)
str = str.replace(pos, num[j].size(), name[j]); // 代码替换汉字
}
outstream << str << endl;
}
return true;
}
//汉字替换代码
void num_to_Chinese(){
//汉字化为代码
string file_path = "2.txt";
string out_path = "3.txt";
ifstream instream("2.txt"); // instream.open(file_path) 默认以ifstream::in打开
ofstream outstream("3.txt"); // outstream.open(out_path) 默认以ostream::out打开,文件内容会被丢弃,可使用app模式(写指针会被定位到文件末尾)
num_FileStringReplace(instream, outstream);
instream.close();
outstream.close();
}
用CSV汉字的代号替换txt中的中文
最新推荐文章于 2025-02-17 19:35:02 发布