C++依次讀取檔案中的漢字並將漢字轉為string型別

pengfoo發表於2013-09-24

 

void Wchar_tToString(std::string& szDst, wchar_t *wchar)
{
	wchar_t * wText = wchar;
	DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的運用
	char *psText; // psText為char*的臨時陣列,作為賦值給std::string的中間變數
	psText = new char[dwNum];
	WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次運用
	szDst = psText;// std::string賦值
	delete []psText;// psText的清除
}

int main()
{


	//將6763個標準漢字讀入TCHAR陣列中
	locale loc( "chs" );
	TCHAR chiWords[CHISIZE+1];
	TCHAR tmpWords[2];

	wifstream file(L"AllChiWords.txt");
	file.imbue(loc);
	if (file.fail())
	{
		return 1;
	}
	file.getline(chiWords, sizeof(chiWords) / sizeof(chiWords[0]));
	//TCHAR last = ch[CHISIZE-1];//最後一個漢字
	file.close();



	for(int i=0;i<CHISIZE;i++)
	{
		string path = "E:\\alljson2\\";
		string s1;
		tmpWords[0] = chiWords[i];
		tmpWords[1] = '\0';
		Wchar_tToString(s1,tmpWords);
		path.append(s1);
		path.append(".json");
		geneChiWordsJson(chiWords[i],path,i);
	}

	//////////////////////////////////////////

}

return 0;
}

相關文章