ofstream 的中文目录问题

问题:
有时候用ifstream或ofstream打开带有中文路径的文件会失败。

例如:

    ofstream outFile("f:\\新建文件夹\\fuck.xml",ios_base::trunc | ios_base::out | ios_base::binary);
    outFile<<"<xml></xml>";
    outFile.close();

解决方法1:

使用wofstream

    wofstream outFile(_T("f:\\新建文件夹\\fuck.xml"),ios_base::trunc | ios_base::out | ios_base::binary);
    outFile<<_T("<xml></xml>");
    outFile.close();

解决方法2:

1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"Chinese-simplified");

2、使用STL函数设置为系统语言环境
std::locale::global(std::locale("")); 

    std::locale::global(std::locale(""));
    ofstream outFile("f:\\新建文件夹\\fuck.xml",ios_base::trunc | ios_base::out | ios_base::binary);
    outFile<<"<xml></xml>";
    outFile.close();
posted @ 2012-04-16 18:06  likebeta  阅读(1098)  评论(0编辑  收藏  举报