[問題] 中文檔名出現亂碼
遇到的問題: (題意請描述清楚)
在儲存txt文字檔時,希望可以用中文當作檔名,但儲存完後檔名卻變成亂碼。
希望得到的正確結果:
不會出現亂碼
程式跑出來的錯誤結果:
亂碼一堆
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
VC 9.0 Windows XP
有問題的code: (請善用置底文標色功能)
str2=ws2s(wstr2); //wstr2儲存中文字串,然後轉成string型態來輸出檔名
str2=str2.append(txt); // 使得中文檔名後面附加上副檔名
ou = new ofstream(str2.c_str(), ofstream::out |ios::binary);//設定輸出的中文檔
我猜大概是這三行有問題,導致我程式結束後,在檔案總管裡看檔名都是亂碼
補充說明:
ws2s()函式裡面寫:
string ws2s(wstring &ws){
int dwNum=WideCharToMultiByte(CP_UTF8,NULL,ws.c_str(),-1,NULL,0,NULL,FALSE);
char *buff = new char[dwNum];
string s;
WideCharToMultiByte(CP_UTF8,NULL,ws.c_str(),-1,buff,dwNum,NULL,FALSE);
s=buff;
delete []buff;
ws.clear();
return s;
}
請問為什麼中文檔名會變成亂碼? 又要怎麼處理呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
→
10/20 14:52, , 1F
10/20 14:52, 1F
→
10/20 14:52, , 2F
10/20 14:52, 2F
→
10/20 14:53, , 3F
10/20 14:53, 3F
→
10/20 14:54, , 4F
10/20 14:54, 4F
推
10/20 15:16, , 5F
10/20 15:16, 5F
→
10/20 15:17, , 6F
10/20 15:17, 6F
→
10/20 15:18, , 7F
10/20 15:18, 7F
→
10/20 15:19, , 8F
10/20 15:19, 8F
推
10/20 15:24, , 9F
10/20 15:24, 9F
→
10/20 15:25, , 10F
10/20 15:25, 10F
※ 編輯: casper67831 來自: 125.231.217.180 (09/29 10:45)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章