好的,我有一個file.txt,其中有這樣的內容:
xzline1
xzline2
當我運行它時,視窗包含以下內容:
xzline1
xzline2
而不是
xzline1
xzline2
不能識別 新行字符,不知道為什么。
我的視窗是這樣定義的
LPCWSTR recordin;
HWND hEdit。
hEdit = CreateWindow(TEXT("EDIT"), NULL,
ws_visible | ws_child | ws_border | ws_hscroll | ws_maximize | es_multiline,
10, 10, 200, 25,
hWnd, (HMENU)NULL, NULL, NULL)。
std::ifstream t("c://file.txt")。
std::stringstream buffer;
buffer << t.rdbuf()。
std::wstring stemp = s2ws(buffer.str();
recordin = stemp.c_str();
SetWindowText(hEdit, recordin);
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() 1;
len = MultiByteToWideChar(CP_ACP, 0, s. c_str(), slength, 0, 0)。)
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len) 。
std::wstring r(buf)。
delete[] buf;
return r;
uj5u.com熱心網友回復:
經典的Windows常用控制元件想要DOS的行結束,。
。 將所有這些
字符轉換成。
。
也許可以作為一個快速的黑客來做這件事:
std::wstring stemp = s2ws(buffer.str()。
// Quick and dirty string copy with DOS to to unix conversions[/span].
std::wstring stemp2;
for (char ch : stemp) {
if (ch == '
') {
stemp2 = "
"。
}
stemp2 = ch;
}
recordin = stemp2.c_str();
SetWindowText(hEdit, recordin)。
另外,你的input.txt很可能被寫成一個標準的Windows文本檔案,并帶有行尾。
行尾的標準 Windows 文本檔案,并且 C 運行時只是將所有這些
實體轉換為字符。
字符。 如果是這種情況,你可以直接將檔案打開為二進制檔案,這樣就不會發生轉換。
替換掉這個:
std::ifstream t("c://file.txt")。
用這個:
std::ifstream t("c://file. txt", std::ios::binary)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/320279.html
標籤:
