fstream 和 中文路径

在利用STL读取文件时,对于中文路径不能识别.参考以下两个位置文档资料解决:

1.解决获取文件大小和文件内容的问题

http://www.cplusplus.com/reference/iostream/istream/tellg.html

该文档给出了文件读取,及取文件大小的方法,但是对于中文目录却会产生问题,(--批注:此例子对于小文件是适用的,

对于大的文件需要循环读取,或利用heap去处理)

// read a file into memory
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  int length;
  char * buffer;

  ifstream is;
  is.open ("test.txt", ios::binary );

  // get length of file:
  is.seekg (0, ios::end);
  length = is.tellg();
  is.seekg (0, ios::beg);

  // allocate memory:
  buffer = new char [length];

  // read data as a block:
  is.read (buffer,length);

  is.close();

  cout.write (buffer,length);

  return 0;
}

2.解决中文路径文件不能读取问题

参考http://www.cppblog.com/mythma/archive/2006/06/09/8349.aspx

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

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

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

在文件中增加 std::locale::global(std::local(""));即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值