匯出簡訊.xml,提取資訊

來源:互聯網
上載者:User

       本博文介紹下如何通過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 32int 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、改進後的運行格式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.