本博文介绍下如何通过C++文件流方式,把从手机导出的.xml格式的短信信息进行处理后存放到磁盘文件
中。对.xml进行的处理包括:
提取发信者姓名、发信时间、短信内容。最后统计下各方发信的条数,以及各方发信的字数。
一、提取出的.xml文件格式如下:
二、处理后的结果如下:
三、处理方法简要介绍如下:
通过对.xml文件分析可以发现,我们需要的信息包括在:
1.<Address>发信者姓名</Address>
2.<Date>发信时间</Date>
3.<Body>短信内容</Body>
所以可以用输入流每次读入一行,并且判断是否是相应标签。若是,则可以做处理;否则,抛弃。对于
统计信息包括各方发送的短信条数、各方发送的短信字数则可以通过设置全局的变量来记录。到底是记录谁
的信息则是通过<Address></Address>标签内的发送者决定。
四、代码如下:
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
#define N 1024
#define nameSize 32
int main()
{
ifstream infile("d:\\a.txt");
ofstream outfile("d:\\b.txt");
if(!infile)
{
cout<<"open source.xml error!"<<endl;
exit(1);
}
if(!outfile)
{
cout<<"open result.txt error!"<<endl;
exit(1);
}
int i;
int sum=0; //短信总条数
int countM=0; //男孩发的短信条数
int countF=0; //女孩发的短信条数
int sumW=0; //短信总字数
int countMW=0; //男孩发的字数
int countFW=0; //女孩发的字数
char temp;
char buffer[N]; //按行读取的缓冲区
char nameM[nameSize]="陆XX";
char nameF[nameSize]="梁XX";
while(!infile.eof())
{
memset(buffer,0,N);
infile.getline(buffer,N); //按行读取
//操作数据,提取出发信者姓名、发信时间、内容
switch(buffer[1])
{
case 'A':
if(buffer[9]=='+')
{
outfile<<nameF<<endl;
countF++;
sum++;
outfile<<countF<<endl;
temp='f';
}
else
{
outfile<<nameM<<endl;
countM++;
sum++;
outfile<<countM<<endl;
temp='m';
}
break;
case 'D':
for(i=6;i<=24;i++)
{
outfile<<buffer[i];
}
outfile<<endl;
break;
case 'B':
string str=buffer;
sumW+=(str.length()-25);
if(temp=='f') countFW+=(str.length()-25);
else countMW+=(str.length()-25);
for(i=15;i<str.length()-10;i++)
{
outfile<<buffer[i];
}
outfile<<endl<<endl;
break;
}
}
//对短信条数进行统计输出
outfile<<endl<<endl<<nameF<<"发的条数: "<<countF<<endl;
outfile<<nameM<<"发的条数: "<<countM<<endl;
outfile<<"总条数: "<<sum<<endl;
//对短信字数进行统计
outfile<<endl<<nameF<<"发的字数: "<<countFW/2<<endl;
outfile<<nameM<<"发的字数: "<<countMW/2<<endl;
outfile<<"总字数: "<<sumW/2<<endl;
return 0;
}
以下内容更新于0:59 2013/7/6
五、代码改进
四中给出的源代码的输入输出文件名及路径是固定的。通过改进使得可以通过命令行参数来更方便的实时
确定路径及文件名。其中 -s 后紧接输入文件路径及名称;-d 后紧接输出文件路径及名称。
1、改进后的代码
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
#define N 1024
#define nameSize 32
#define timeSize 20
/*******************************改进部分***********************************/
int main(int argc,char *argv[])
{
int souIndex=0,destIndex=0; //源文件地址和目的文件地址的下标
for(int index=1;index<argc;index++)
{
if(argv[index][0]=='-' && argv[index][1]=='s')
{
index++;
souIndex=index;
}
else if(argv[index][0]=='-' && argv[index][1]=='d')
{
index++;
destIndex=index;
}
}
/****************************************************************************/
ifstream infile(argv[souIndex]); //打开源文件
ofstream outfile(argv[destIndex]); //打开目的文件
if(!infile)
{
cout<<"open source.xml error!"<<endl;
exit(1);
}
if(!outfile)
{
cout<<"open result.txt error!"<<endl;
exit(1);
}
int i;
int sum=0; //短信总条数
int countM=0; //男孩发的短信条数
int countF=0; //女孩发的短信条数
int sumW=0; //短信总字数
int countMW=0; //男孩发的字数
int countFW=0; //女孩发的字数
char temp;
char buffer[N]; //按行读取的缓冲区
char nameM[nameSize]="陆XX";
char nameF[nameSize]="梁XX";
char timeSave[timeSize]; //记录最后一条短信发送的时间
while(!infile.eof())
{
memset(buffer,0,N);
infile.getline(buffer,N); //按行读取
//操作数据,提取出发信者姓名、发信时间、内容
switch(buffer[1])
{
case 'A':
if(buffer[9]=='+')
{
outfile<<nameF<<endl;
countF++;
sum++;
outfile<<countF<<endl;
temp='f';
}
else
{
outfile<<nameM<<endl;
countM++;
sum++;
outfile<<countM<<endl;
temp='m';
}
break;
case 'D':
for(i=6;i<=24;i++)
{
outfile<<buffer[i];
timeSave[i-6]=buffer[i];
}
outfile<<endl;
break;
case 'B':
string str=buffer;
sumW+=(str.length()-25);
if(temp=='f') countFW+=(str.length()-25);
else countMW+=(str.length()-25);
for(i=15;i<str.length()-10;i++)
{
outfile<<buffer[i];
}
outfile<<endl<<endl;
break;
}
}
//截止日期
outfile<<endl<<endl<<"短信截止时间: "<<endl;
for(i=0;i<19;i++)
{
outfile<<timeSave[i];
}
//对短信条数进行统计输出
outfile<<endl<<endl<<nameF<<"发的条数: "<<countF<<endl;
outfile<<nameM<<"发的条数: "<<countM<<endl;
outfile<<"总条数: "<<sum<<endl;
//对短信字数进行统计
outfile<<endl<<nameF<<"发的字数: "<<countFW/2<<endl;
outfile<<nameM<<"发的字数: "<<countMW/2<<endl;
outfile<<"总字数: "<<sumW/2<<endl;
return 0;
}
2、改进后的运行格式