VS2008下fstream操作文件时的中文路径
问题:
有时候用VC6ofstream或ofstream打开带有中文路径的文件会成功,但在VS2008下会失败。
解决办法:
1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"Chinese-simplified");
setlocale(LC_ALL,"");
2、使用STL函数设置为系统语言环境
std::locale::global(std::locale(""));
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
printf("Hello World!/n");
string FileName("C://在//a.txt");
std::locale::global(std::locale(""));
static std::ofstream logfile(FileName.c_str(), std::ios_base::out | std::ios_base::app);
static bool logfile_is_open = logfile.is_open();
if (logfile_is_open)
//....;
return 0;
}