码迷,mamicode.com
首页 > 其他好文 > 详细

STL的string如何使用UNICODE?

时间:2016-05-13 02:12:25      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

#include "stdafx.h"
#include "TestCmd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CWinApp theApp;

using namespace std;


#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif


// 唯一的应用程序对象


static void WriteString(HANDLE f, LPCTSTR lpsz, int len);

void WriteString(HANDLE f, LPCTSTR lpsz, int len)
{
	DWORD nWrite = len * sizeof(TCHAR);
	DWORD nActual;
	if (WriteFile(f, lpsz, nWrite, &nActual, NULL)) {
		// display results.
		_tprintf(_T("%d bytes written\n sizeof(TCHAR)=%d\n"), nActual, 
			sizeof(TCHAR));
	} else {
		_tprintf(_T("ERROR %d writing\n"), GetLastError());
	}
}



int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// 初始化MFC 并在失败时显示错误
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: 更改错误代码以符合您的需要
		_tprintf(_T("错误: MFC 初始化失败\n"));
		nRetCode = 1;
	}
	else
	{

		// CreateFile will create Unicode or MBCS string
		// depending on value of _UNICODE.
		LPCTSTR filename = _T("D:\\1.txt");
		HANDLE f = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 
			FILE_SHARE_READ|FILE_SHARE_WRITE,
			NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

		if (f!=INVALID_HANDLE_VALUE) 
		{
			if (GetFileType(f) == FILE_TYPE_DISK)
			{
				// create STL tstring
				tstring s = _T("Hello, world");
				WriteString(f, s.c_str(), s.length());
			} else {
				_tprintf(_T("ERROR: the specified file '%s' is not a disk file\n"),
					filename);
			}
			CloseHandle(f); // close file
		} else {
			_tprintf(_T("ERROR: can't open '%s'\n"), filename);
		}


	}

	return nRetCode;
}






STL的string如何使用UNICODE?

标签:

原文地址:http://blog.csdn.net/dijkstar/article/details/51347581

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!