上上周把手机丢了,可是原先备份的通讯录只有一个.vcf文件,里面含盖了我所有的联系人信息比如100个联系人信息他全存进一个.vcf文件了。换了新手机,怎么也导不到新手机里面,原因是新手机只支持一个.vcf文件里存一条通讯录的那种格式,比如100个联系人信息就得有100个.vcf文件放到内存卡里才能还原。
我用文本方式观察了一下,正好可以用c++处理一下,正好也练一个文件处理方面的基本功。代码如下,vc6.0编译。
#include <iostream>
#include <fstream>
#include <string>using namespace std;int main()
{ifstream first("D:\\myPro\\phoneBook\\a.vcf",ios::in);//读取的方式打开文件if(first)cout<<"文件打开成功"<<endl;elsecout<<"文件打开失败"<<endl;string result;string temp;string name;while(!first.eof()){char buf[100];first.get(buf,100,'\n'); //读取一行到buftemp=buf;result+=temp+"\n";if(temp.find("N:")!=string::npos&&temp.find("BEGIN")==string::npos&&temp.find("FN")==string::npos&&temp.find("VERSION:")==string::npos){name.assign(temp,2,temp.length()-2);}if(temp.find("END")!=string::npos){cout<<"记录为\n"<<result<<endl;cout<<"---------------------------------\n";name=name+".vcf";const char *str=name.c_str();ofstream second(str);//以人物姓名创建一个文件second<<result;second.close();result="";name="";}memset(buf,0,sizeof(buf));first.seekg(2,ios::cur);}return 0;
}