xml文件 <-> txt文件 c++
#include <iostream>
#include <fstream*>
#include <string*>
#include <vector*>
#include <algorithm*>
using namespace std;
///实际上四个find函数可合并为一个函数,只需增加两个形参strbegin和strend使用字符串匹配算法即可/
int find_id(const string&);//查找id所在
int find_name(const string&);
int find_course(const string&);
int find_score(const string&);
int ASCALL_TO_INT(const char&);//成绩转换为int型,便于比较
/另外Sortstruct同样可以进行字符串比较 比如a.strlen() 和 b.strlen() 大小 相等则一位位比较/
///学生相关信息类/
class student {//类存储对象
public:
string id;
string name;
string course;
int score = 0;
};
bool Sortstruct(const student*, const student*); //配合sort函数指定vector排序规则
///主函数/
int in_xml_out_txt()
{
ifstream ifs;//创建读文件对象
ofstream ofs;//创建写文件独享
ifs.open(“D:/桌面/book.xml”, ios::in);
ofs.open(“D:/桌面/file.txt”, ios::out | ios::ate);
vector<student*> stuptrs;//创建vector容器保存学生类对象指针 向量可用sort
string a;
char temp[20];
memset(temp, '\0', 20);//初始化temp字符串 memset对字符串全部赋值\0
while (getline(ifs, a)){//若到文本末则退出
int i = find_id(a);//调用find函数找到 id所在行
if (i == -1)
continue;//如果i=-1;表示当前行不存在id则直接进行下一轮
student* stuptr = new student;
stuptrs.push_back(stuptr);//存入vector
/*拷贝ID*/
for (int j = 0; j < 7; j++)//学号固定7位数
temp[j] = a[i + j];
stuptr->id = temp;//将temp中的学号保存至当前学生对象 可将char[]直接赋值给 string
memset(temp, '\0', 20);//初始化temp;
/*拷贝名字*/
getline(ifs, a);
i = find_name(a);
for (int j = 0; a[i + j] != '<'; j++)
temp[j] = a[i + j];
stuptr->name = temp;
memset(temp, '\0', 20);
/*拷贝课程*/
getline(ifs, a);
i = find_course(a);
for (int j = 0; a[i + j] != '<'; j++)
temp[j] = a[i + j];
stuptr->course = temp;
memset(temp, '\0', 20);
/*拷贝分数*/
getline(ifs, a);
i = find_score(a);
for (int j = 0; a[i + j] != '<'; j++)
temp[j] = a[i + j];
if (temp[1] == '\0')//调用转换函数确定分数
stuptr->score = ASCALL_TO_INT(temp[0]);//一位数
else if (temp[2] == '\0')
stuptr->score = ASCALL_TO_INT(temp[0]) * 10 + ASCALL_TO_INT(temp[1]);
else
stuptr->score = 100;//满分
memset(temp, '\0', 20);
}
//调用algorithm头文件中的sort函数,按Sortstruct规则排序
sort(stuptrs.begin(), stuptrs.end(), Sortstruct);//将指针当作数据类型,正常进行交换排序
/*遍历写入txt并删除新建的结构体内存*/
for (auto n : stuptrs) {
ofs << n->id << ',' << n->name << ',' << n->course << ',' << n->score << endl;
cout<< n->id << ',' << n->name << ',' << n->course << ',' << n->score << endl;
delete n;
}
return 0;
}
int find_id(const string& a) {
int flag = -1;
int i = 0;
for (i; a[i + 3] != ‘\0’; i++) {//
if (a[i] == ‘<’ && a[i + 1] == ‘i’ && a[i + 2] == ‘d’ && a[i + 3] == ‘>’) {
flag = i + 4;
break;
}
}
return flag;//返回的非 -1说明这一行是数据开头 学号
}
int find_name(const string& a) {
int flag = -1;
int i = 0;
for (i; a[i + 5] != ‘\0’; i++) {
if (a[i] == ‘<’ && a[i + 1] == ‘n’ && a[i + 2] == ‘a’
&& a[i + 3] == ‘m’ && a[i + 4] == ‘e’ && a[i +
5] == ‘>’) {
flag = i + 6;
break;
}
}
return flag;
}
int find_course(const string& a) {
int flag = -1;
int i = 0;
for (i; a[i + 7] != ‘\0’; i++) {
if (a[i] == ‘<’ && a[i + 1] == ‘c’ && a[i + 2] == ‘o’
&& a[i + 3] == ‘u’ && a[i + 4] == ‘r’ && a[i +
5] == ‘s’ && a[i + 6] == ‘e’ && a[i + 7] == ‘>’) {
flag = i + 8;
break;
}
}
return flag;
}
int find_score(const string& a) {
int flag = -1;
int i = 0;
for (i; a[i + 6] != ‘\0’; i++) {
if (a[i] == ‘<’ && a[i + 1] == ‘s’ && a[i + 2] == ‘c’
&& a[i + 3] == ‘o’ && a[i + 4] == ‘r’ && a[i +
5] == ‘e’ && a[i + 6] == ‘>’) {
flag = i + 7;
break;
}
}
return flag;
}
int ASCALL_TO_INT(const char& ASCALL)
{
return ASCALL - ‘0’;//将一位的 char转为int数字
}
bool Sortstruct(const student* stu1, const student* stu2)
{//排序函数 设置为降序
return stu1->score > stu2->score;// 不传这个函数也能对结构体进行比较(升序)???
}
bool Sortstruct_asc(const student* stu1, const student* stu2)
{//排序函数 设置为升序
return stu1->score < stu2->score;//
}
//读入txt 并根据最后一列大小排序 后写入xml文件
int find_1(char str[]) {//找到第1个’,‘所在位置
int position = 0;
while (str[position]!=’\0’) {
if (str[position++] == ‘,’) {
break;
}
}
return position;
}
int find_2(char str[]) {//找到第2个’,‘所在位置
int position = 0; int count = 0;
while (str[position] != ‘\0’) {
if (str[position++] == ‘,’) {
count++;
if (count == 2)break;
}
}
return position;
}
int find_3(char str[]) {//找到第3个’,'所在位置
int position = 0; int count = 0;
while (str[position] != ‘\0’) {
if (str[position++] == ‘,’) {
count++;
if (count == 3)break;
}
}
return position;
}
void in_txt_out_xml() {
FILE* p;
fopen_s(&p, “D:/桌面/file.txt”, “r”);
if (p == nullptr)cout << “read error”;
vector<student*> stuptrs;//创建vector容器保存学生类对象指针 向量可用sort
char str[60]; memset(str, '\0', 60);//初始化
char t[20]; memset(t, '\0', 20);
while (fgets(str, 60, p)) {
cout << str;
int i = 0;
int position1 = find_1(str);//找到姓名所在位置
int position2 = find_2(str);//找到课程所在位置
int position3 = find_3(str);//找到分数所在位置
//cout << position1 << " " << position2 <<" "<< position3<<endl;
//student* s = (student*)malloc(sizeof(student)); //×会报错
student* s = new student;//√
stuptrs.push_back(s);
while (i < 7) {
t[i] = str[i];
i++;
}
s->id = t;//得到学号
memset(t, '\0', 20);//以 \0 初始化
i = 0;
while (str[i + position1]!=',') {
t[i] = str[i + position1];
i++;
}s->name = t;
i = 0; memset(t, '\0', 20);
while (str[i + position2]!=',') {
t[i] = str[i + position2 ];
i++;
}s->course = t;
i = position3; memset(t, '\0', 20);//分数
//cout << str[i+2]<<endl;
int grade = 0;//最后一个是换行符 而不是\0!
if (str[i + 2] != '\n') {
grade = 100;
}
else if(str[i + 1] != '\n'){
grade = 10 * ASCALL_TO_INT(str[i]) + ASCALL_TO_INT(str[i + 1]);
}
else grade = ASCALL_TO_INT(str[i]);//将单个char转为int
s->score = grade;
memset(t, '\0', 20);
}
fclose(p);
cout << endl;
sort(stuptrs.begin(), stuptrs.end(), Sortstruct_asc);//默认升序,降序排列+ ,Sortstruct_asc
//fopen_s(&p, "D:/桌面/file.txt", "w"); fwrite(str,sizeof(str),1, p);
ofstream ofs;//创建写文件独享
ofs.open("D:/桌面/inXml.xml", ios::out | ios::ate);//写入txt
ofs << "<grades>" << endl;
for (auto n : stuptrs) {
ofs << "<grade>" << endl;
ofs << "<id>" << n->id << "</id>" << endl;
ofs << "<name>" << n->name << "</name>" << endl;
ofs << "<course>" << n->course << "</course>" << endl;
ofs << "<score>" << n->score << "</score>" << endl;
cout << n->id << ',' << n->name << ',' << n->course << ',' << n->score << endl;
delete n;
ofs << "</grade>" << endl;
}
ofs << "</grades>" << endl;
}
int main() {
in_xml_out_txt();//读入xml 输出为txt
cout << endl;
in_txt_out_xml();//读入txt 输出为xml
return 1;
}