#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; }
时间: 2024-11-05 04:49:03