//========================================================================
//TITLE:
// vcf文件分解
//AUTHOR:
// norains
//DATE:
// Saturday 4-October-2008
//Environment:
// NONE
//========================================================================
不知为何,索爱K750再也起不来了,只好去买款NOKIA 5320。所幸的是,之前索爱K750有一部分联系人是存在PC的的玩转手机这款软件中,只是没想到玩转手机是将所有的联系人全部导出到一个文件,而我的5320对同一个文件只能识别一个联系人。无奈,顺手写了个程序,将同一个文件中的联系人提取出来,分别保存为同名文件。
因为只是简单的提取,方便自己,所以路径是固定的。如果各位朋友打算使用该代码,可以将路径改为相对应的文件。当然,最好是做一个弹出对话框,选择文件了,呵呵~
代码如下;
// VCardConvert.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "string"
#include "vector"
#include "map"
#define STR_BEGIN_FLAG "BEGIN:VCARD"
#define STR_END_FLAG "END:VCARD"
#define STR_NAME_FLAG ";"
#define STR_FILE_READ "H://电话簿.vcf"
#define STR_SAVE_FOLDER "h://电话本//"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
HANDLE hFileRead = CreateFile(STR_FILE_READ,GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFileRead == INVALID_HANDLE_VALUE)
{
return 0x01;
}
//The read buffer
std::vector<char> vtBuf(GetFileSize(hFileRead,NULL),0);
//Read the file
DWORD dwRead = 0;
ReadFile(hFileRead,&vtBuf[0],vtBuf.size(),&dwRead,NULL);
if(dwRead == 0)
{
return 0x02;
}
//The buffer for finding
std::string strBuf(vtBuf.begin(),vtBuf.end());
std::map<std::string,std::string> mpWrite;
//std::vector<std::string> vtStore;
//Store the content
std::string::size_type posBegin = strBuf.find(STR_BEGIN_FLAG,0);
std::string::size_type posEnd = 0;
while(posBegin != std::string::npos)
{
posEnd = strBuf.find(STR_END_FLAG,posBegin);
if(posEnd == std::string::npos)
{
break;
}
std::string::size_type posNameBegin = strBuf.find(STR_NAME_FLAG,posBegin);
std::string::size_type posNameEnd = strBuf.find(STR_NAME_FLAG,posNameBegin + 1);
if(posNameBegin != std::string::npos && posNameEnd != std::string::npos)
{
mpWrite.insert(std::make_pair(std::string(strBuf.begin() + posNameBegin + 1, strBuf.begin() + posNameEnd),
std::string(strBuf.begin() + posBegin, strBuf.begin() + posEnd + strlen(STR_END_FLAG))));
}
posBegin = strBuf.find(STR_BEGIN_FLAG,posEnd);
//vtStore.push_back(std::string(strBuf.begin() + posBegin, strBuf.begin() + posEnd + strlen(STR_END_FLAG)));
}
CloseHandle(hFileRead);
//Write the text
for(std::map<std::string,std::string>::iterator iter = mpWrite.begin(); iter != mpWrite.end(); iter ++)
{
std::string strFile = STR_SAVE_FOLDER;
strFile += iter->first;
strFile += ".vcf";
HANDLE hFileWrite = CreateFile(strFile.c_str(),GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFileWrite == INVALID_HANDLE_VALUE)
{
return 0x01;
}
DWORD dwWrite = 0;
WriteFile(hFileWrite,iter->second.c_str(),iter->second.size(),&dwWrite,NULL);
CloseHandle(hFileWrite);
}
return 0;
}