//Create textfile CTextFileWrite::CTextFileWrite(const FILENAMECHAR* filename, TEXTENCODING encoding) { #if PEK_TX_TECHLEVEL == 0 m_file.open(filename, ios::binary | ios::out ); #elif PEK_TX_TECHLEVEL == 1 m_hFile = ::CreateFile( filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); #else m_file = new CFile; m_file->Open(filename, CFile::modeCreate | CFile::modeWrite); m_closeAndDeleteFile = true; #endif m_buffpos = -1; m_buffsize = 0; m_encoding = encoding; WriteBOM(); }
bool wxUtfFile::Create(const wxChar *szFileName, bool bOverwrite, int access, wxFontEncoding encoding) { if (!wxFile::Create(szFileName, bOverwrite, access)) return false; DetermineConversion(encoding); WriteBOM(); return true; }
bool wxUtfFile::Open(const wxChar *szFileName, OpenMode mode, int access, wxFontEncoding encoding) { if (!wxFile::Open(szFileName, mode, access)) return false; m_bomOffset = 0; DetermineConversion(wxFONTENCODING_SYSTEM); if (mode != write && EvalBOM(encoding)) { // File freshly created, need BOM if (mode != read) WriteBOM(); } else if (mode != read && encoding != wxFONTENCODING_DEFAULT) { // force BOM to a specific value switch (encoding) { case wxFONTENCODING_UTF8: case wxFONTENCODING_UTF16BE: case wxFONTENCODING_UTF16LE: case wxFONTENCODING_UTF32BE: case wxFONTENCODING_UTF32LE: break; default: encoding = wxFONTENCODING_SYSTEM; break; } if (encoding != m_encoding) { DetermineConversion(encoding); WriteBOM(); } } return true; }
//Create textfile from CFile object CTextFileWrite::CTextFileWrite(CFile* file, TEXTENCODING encoding) { ASSERT(file); m_file = file; m_closeAndDeleteFile = false; m_encoding = encoding; m_buffpos = -1; m_buffsize = 0; WriteBOM(); }
//Create textfile CTextFileWrite::CTextFileWrite(const FILENAMECHAR* filename, TEXTENCODING encoding) { #ifndef PEK_TX_USEMFC m_file.open(filename, ios::binary | ios::out ); #else m_file = new CFile; m_file->Open(filename, CFile::modeCreate | CFile::modeWrite); m_closeAndDeleteFile = true; #endif m_buffpos = -1; m_buffsize = 0; m_encoding = encoding; WriteBOM(); }
bool create_bob_txt() { // current directory 를 구한다. wchar_t *buf = NULL; uint32_t buflen = 0; buflen = GetCurrentDirectoryW(buflen, buf); if (0 == buflen) { print("err, GetCurrentDirectoryW() failed. gle = 0x%08x", GetLastError()); return false; } buf = (PWSTR)malloc(sizeof(WCHAR)* buflen); if (0 == GetCurrentDirectoryW(buflen, buf)) { print("err, GetCurrentDirectoryW() failed. gle = 0x%08x", GetLastError()); free(buf); return false; } // current dir \\ bob.txt 파일명 생성 wchar_t file_name[260]; if (!SUCCEEDED(StringCbPrintfW( file_name, sizeof(file_name), L"%ws\\bob.txt", buf))) { print("err, can not create file name"); free(buf); return false; } free(buf); buf = NULL; if (true == is_file_existsW(file_name)) { ::DeleteFileW(file_name); } // 파일 생성 HANDLE file_handle = CreateFileW( file_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if (file_handle == INVALID_HANDLE_VALUE) { print("err, CreateFile(path=%ws), gle=0x%08x", file_name, GetLastError()); return false; } // 파일에 데이터 쓰기 DWORD bytes_written = 0; wchar_t string_buf[1024]; if (!SUCCEEDED(StringCbPrintfW( string_buf, sizeof(string_buf), L"동해물과 백두산이 마르고 닳도록 하느님이 보우하사 우리나라만세"))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } char string_buf_mb[1024]; WriteBOM(file_handle); WideCharToMultiByte(CP_UTF8, 0, string_buf, -1, string_buf_mb, sizeof(string_buf_mb), NULL, FALSE); // if (!WriteFile(file_handle, string_buf, wcslen(string_buf), &bytes_written, NULL)) if (!WriteFile(file_handle, string_buf_mb, strlen(string_buf_mb), &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } // 영어로 쓰기 if (!SUCCEEDED(StringCbPrintfW( string_buf, sizeof(string_buf), L"All work and no play makes jack a dull boy."))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } WideCharToMultiByte(CP_UTF8, 0, string_buf, -1, string_buf_mb, sizeof(string_buf_mb), NULL, FALSE); // if (!WriteFile(file_handle, string_buf, wcslen(string_buf), &bytes_written, NULL)) if (!WriteFile(file_handle, string_buf_mb, strlen(string_buf_mb), &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } char string_bufa[1024]; if (!SUCCEEDED(StringCbPrintfA( string_bufa, sizeof(string_bufa), "동해물과 백두산이 마르고 닳도록 하느님이 보우하사 우리나라만세"))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } wchar_t string_buf_16[1024]; MultiByteToWideChar(CP_ACP, 0, string_bufa, -1, string_buf_16, sizeof(string_buf_16)); WideCharToMultiByte(CP_UTF8, 0, string_buf_16, -1, string_bufa, sizeof(string_buf_mb), NULL, FALSE); if (!WriteFile(file_handle, string_bufa, strlen(string_bufa), &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } // 영어로 쓰기 if (!SUCCEEDED(StringCbPrintfA( string_bufa, sizeof(string_bufa), "All work and no play makes jack a dull boy."))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } MultiByteToWideChar(CP_ACP, 0, string_bufa, -1, string_buf_16, sizeof(string_buf_16)); WideCharToMultiByte(CP_UTF8, 0, string_buf_16, -1, string_bufa, sizeof(string_buf_mb), NULL, FALSE); if (!WriteFile(file_handle, string_bufa, strlen(string_bufa), &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } // 파일 닫기 CloseHandle(file_handle); DeleteFileW(file_name); return true; }