下面是代碼:ENDL不與wstring的(Unicode)的工作
std::ofstream f("file1.txt");
f<<"123"<<std::endl<<"456"; //(*1)
/*std::stringstream ordinary_strstream; This works too
ordinary_strstream<<"123"<<'\n'<<"456";
f<<ordinary_strstream.str();*/
std::wstringstream s;
s<<L"123"<<std::endl<<L"456"; //(*2)
s<<L"123"<<L"\n"<<L"456"; //(*3)
s<<"123"<<WCHAR(13)<<WCHAR(10)<<"456";//(*4)
HANDLE h =CreateFileW(L"file2.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
ULONG l;
WriteFile(h, s.str().c_str(), s.str().length() * 2, &l, NULL);
在(* 1)的情況下有一個新行,在(* 2)和(* 3)我看到在file2.txt中沒有換行符。在(* 3)中有一個換行符。我使用notepad.exe
進行瀏覽。十六進制編輯器沒有顯示0x0D
字節,只有0x0A
。
我應該如何正確把unline文本文件中的換行符?謝謝。
'2'應該是'sizeof(wchar_t)',不是嗎? – Fanael 2012-04-28 12:46:38
''2''只是用於縮略,在原始代碼中當然有std :: wstring :: value_type。 – fogbit 2012-04-28 12:49:27
小心你的術語。 Unicode是代碼點的規範。從技術上講,您在文件中存儲的是Unicode的編碼。如果你使用Windows和wchar_t,這可能是UTF-16 – 2012-04-28 13:34:20