VS环境Unicode模式下,CString输出到FILE文件类中

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haimianjie2012/article/details/82805150

当VS2010环境中字符集采用Use Unicode Character Set模式时,Cstring 转换为Const char*具体代码如下:

const size_t strSize=(m_VectorKey[k].GetLength()+1)*2;
		char* pstr=new char[strSize];
		size_t sz=0;
		wcstombs_s(&sz,pstr,strSize,m_VectorKey[k],_TRUNCATE);
		const char* pcstr=(const char*)pstr;

将Vector中的CString输出到FILE文件中:

std::vector<CString> m_VectorKey;
	std::vector<CString> m_VectorValue;

	m_VectorValue.push_back(L"123");
	m_VectorValue.push_back(L"456");
	m_VectorKey.push_back(L"key1:");
	m_VectorKey.push_back(L"key2:");
	FILE* fp;
	fp = fopen("D:\\tmp\\data.txt", "w");

	for(int k = 0 ; k< m_VectorKey.size(); ++k)
	{
		const size_t strSize=(m_VectorKey[k].GetLength()+1)*2;
		char* pstr=new char[strSize];
		size_t sz=0;
		wcstombs_s(&sz,pstr,strSize,m_VectorKey[k],_TRUNCATE);
		
		fprintf(fp,(const char*)pstr);
		fprintf(fp,"\t");


		const size_t strSize1=(m_VectorValue[k].GetLength()+1)*2;
		char* pstr1=new char[strSize1];
		size_t sz1=0;
		wcstombs_s(&sz1,pstr1,strSize1,m_VectorValue[k],_TRUNCATE);
		fprintf(fp,(const char*)pstr1);
		fprintf(fp,"\n");
	}
	fprintf(fp,"\n");
	fprintf(fp,"***m_VectorKey****\n");
	fclose (fp);

猜你喜欢

转载自blog.csdn.net/haimianjie2012/article/details/82805150