bool create_bob_txt() { wchar_t *buf = getCurrentDir(); // current dir \\ bob.txt 파일명 생성 wchar_t file_name[260]; if (!SUCCEEDED(StringCbPrintfW( // safe하도록 모든 boundary check를 다 해주는 API file_name, sizeof(file_name), L"%ws\\bob.txt", buf))) { print("err, cannot 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 cbWritten = 0; BYTE ByteOrderMark[] = { 0xEF, 0xBB, 0xBF }; if (TRUE != WriteFile(file_handle, ByteOrderMark, sizeof(ByteOrderMark), &cbWritten, NULL)) { print("err, BOM writing failed, gle=0x%08x", GetLastError()); CloseHandle(file_handle); return false; } wchar_t file_content[] = L"안녕하십니까. 이 테이프는 2015학년도 대학수학능력시험 언어 영역 듣기 평가 안내 녹음테이프입니다. 지금부터 2015학년도 대학수학능력시험 1교시 언어 영역 듣기 방송을 시작하겠습니다."; char *utf8_file_content = WcsToMbsUTF8(file_content); WriteFile(file_handle, utf8_file_content, strlen(utf8_file_content), &cbWritten, NULL); wchar_t file_content2[] = L"'Cause baby now we got bad blood. You know it used to be mad love. So take a look what you've done. 'Cause baby now we got bad blood."; utf8_file_content = WcsToMbsUTF8(file_content2); WriteFile(file_handle, utf8_file_content, strlen(utf8_file_content), &cbWritten, NULL); free(utf8_file_content); // 파일 닫기 CloseHandle(file_handle); return true; }
/** * @brief * @param * @see * @remarks * @code * @endcode * @return */ std::string WcsToMbsUTF8Ex(_In_ const wchar_t *wcs) { std::auto_ptr<char> tmp(WcsToMbsUTF8(wcs)); if (NULL == tmp.get()) { return _null_stringa; } else { return std::string(tmp.get()); } }