上上周把手机丢了,可是原先备份的通讯录只有一个.vcf文件,里面含盖了我所有的联系人信息比如100个联系人信息他全存进一个.vcf文件了。换了新手机,怎么也导不到新手机里面,原因是新手机只支持一个.vcf文件里存一条通讯录的那种格式,比如100个联系人信息就得有100个.vcf文件放到内存卡里才能还原。
我用文本方式观察了一下,正好可以用c++处理一下,正好也练一个文件处理方面的基本功。代码如下,vc6.0编译。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#include <iostream>
#include <fstream>
#include <string>
using
namespace
std;
int
main()
{
ifstream first(
"D:\\myPro\\phoneBook\\a.vcf"
,ios::in);
//读取的方式打开文件
if
(first)
cout<<
"文件打开成功"
<<endl;
else
cout<<
"文件打开失败"
<<endl;
string result;
string temp;
string name;
while
(!first.eof())
{
char
buf[100];
first.get(buf,100,
'\n'
);
//读取一行到buf
temp=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;
}
|