#ifndef UNICODE
#define UNICODE
#endif
#include <Windows.h>
#include <cstdio>
#include <fstream>
using namespace std;
int main()
{
FILE* resFile;
char multiByteStr[256];
ifstream oFile;
FILE* exampleFile;
TCHAR buffer[256];
system("chcp 65001");
resFile = _wfopen(L"foo",L"w, ccs=UTF-8");
fwprintf(resFile,L"%s",L"C:\\exsistingFolder\\zażółć gęśłą jaźń ☺☻♥♦• ć.txt");
fclose(resFile);
oFile.open(L"foo");
oFile.getline(multiByteStr,256,'\n');
oFile.close();
MultiByteToWideChar(CP_UTF8,0,multiByteStr,256,buffer,256);
wprintf(L"%s",buffer);
exampleFile = _wfopen(buffer,L"w, ccs=UTF-16LE");
fwprintf(exampleFile,L"%s",buffer);
fclose(exampleFile);
system("pause");
return 0;
}
正如你所看到的,程序應該創建文件「foo」的resFile
包含該文件的完整路徑創建,並且這個新的文件exampleFile
應該包含對自身的路徑。儘管在Visual Studio 2010自動調試期間,該緩衝區具有正確的字符串,但不會創建exampleFile。爲什麼?
另一件事:爲什麼wprintf
不輸出擴展字符,儘管我已將控制檯的字體切換到Lucida控制檯 - 可以處理uncode字符的字體。UTF-8到寬字符轉換
詩篇。 exampleFile指向NULL
,即使在_wfopen
之後,緩衝區的最後一個字符也是'/0'
。
'buffer' NULL終止了嗎?你是否在'oFile.close'後檢查了調用的返回值? – dirkgently
[製作寬字符文件](http://stackoverflow.com/questions/10769023/making-wide-char-file) –
要將Unicode打印到控制檯,除了'wprintf'之外,還需要'_setmode' (但不包括'chcp') - 請參閱http://blogs.msdn.com/b/michkap/archive/2008/03/18/8306597.aspx。 – Philipp