xml文件 <-> txt文件 c++
#include <iostream>
#include <fstream*>
#include <string*>
#include <vector*>
#include <algorithm*>
using namespace std;
///實際上四個find函數(shù)可合并為一個函數(shù),只需增加兩個形參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&);//成績轉(zhuǎn)換為int型,便于比較
/另外Sortstruct同樣可以進行字符串比較 比如a.strlen() 和 b.strlen() 大小 相等則一位位比較/
///學(xué)生相關(guān)信息類/
class student {//類存儲對象
public:
string id;
string name;
string course;
int score = 0;
};
bool Sortstruct(const student*, const student*); //配合sort函數(shù)指定vector排序規(guī)則
///主函數(shù)/
int in_xml_out_txt()
{
ifstream ifs;//創(chuàng)建讀文件對象
ofstream ofs;//創(chuàng)建寫文件獨享
ifs.open(“D:/桌面/book.xml”, ios::in);
ofs.open(“D:/桌面/file.txt”, ios::out | ios::ate);
vector<student*> stuptrs;//創(chuàng)建vector容器保存學(xué)生類對象指針 ?向量可用sort
string a;
char temp[20];
memset(temp, '\0', 20);//初始化temp字符串 memset對字符串全部賦值\0
while (getline(ifs, a)){//若到文本末則退出
int i = find_id(a);//調(diào)用find函數(shù)找到 id所在行
if (i == -1)
continue;//如果i=-1;表示當(dāng)前行不存在id則直接進行下一輪
student* stuptr = new student;
stuptrs.push_back(stuptr);//存入vector
/*拷貝ID*/
for (int j = 0; j < 7; j++)//學(xué)號固定7位數(shù)
temp[j] = a[i + j];
stuptr->id = temp;//將temp中的學(xué)號保存至當(dāng)前學(xué)生對象 可將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);
/*拷貝分?jǐn)?shù)*/
getline(ifs, a);
i = find_score(a);
for (int j = 0; a[i + j] != '<'; j++)
temp[j] = a[i + j];
if (temp[1] == '\0')//調(diào)用轉(zhuǎn)換函數(shù)確定分?jǐn)?shù)
stuptr->score = ASCALL_TO_INT(temp[0]);//一位數(shù)
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);
}
//調(diào)用algorithm頭文件中的sort函數(shù),按Sortstruct規(guī)則排序
sort(stuptrs.begin(), stuptrs.end(), Sortstruct);//將指針當(dāng)作數(shù)據(jù)類型,正常進行交換排序
/*遍歷寫入txt并刪除新建的結(jié)構(gòu)體內(nèi)存*/
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說明這一行是數(shù)據(jù)開頭 學(xué)號
}
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(chǎn)’
&& 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轉(zhuǎn)為int數(shù)字
}
bool Sortstruct(const student* stu1, const student* stu2)
{//排序函數(shù) 設(shè)置為降序
return stu1->score > stu2->score;// 不傳這個函數(shù)也能對結(jié)構(gòu)體進行比較(升序)???
}
bool Sortstruct_asc(const student* stu1, const student* stu2)
{//排序函數(shù) 設(shè)置為升序
return stu1->score < stu2->score;//
}
//讀入txt 并根據(jù)最后一列大小排序 后寫入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;//創(chuàng)建vector容器保存學(xué)生類對象指針 ?向量可用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);//找到分?jǐn)?shù)所在位置
//cout << position1 << " " << position2 <<" "<< position3<<endl;
//student* s = (student*)malloc(sizeof(student)); //×?xí)箦e
student* s = new student;//√
stuptrs.push_back(s);
while (i < 7) {
t[i] = str[i];
i++;
}
s->id = t;//得到學(xué)號
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);//分?jǐn)?shù)
//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轉(zhuǎn)為int
s->score = grade;
memset(t, '\0', 20);
}
fclose(p);
cout << endl;
sort(stuptrs.begin(), stuptrs.end(), Sortstruct_asc);//默認(rèn)升序,降序排列+ ,Sortstruct_asc
//fopen_s(&p, "D:/桌面/file.txt", "w"); fwrite(str,sizeof(str),1, p);
ofstream ofs;//創(chuàng)建寫文件獨享
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;
}