利用标准c++流读文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void do(){
std::string tmppath = "aa.zip";
std::ifstream is(tmppath.c_str(), std::ios_base::binary, _SH_DENYRW);

std::streampos current_pos1 = is.tellg();//记录下当前位置
is.seekg(0, std::ios::end);//移动到文件尾
std::streampos nSize = is.tellg();
is.seekg(current_pos1);
std::cout << nSize << std::endl;

is.clear();
is.seekg(0);

std::string fileContent;
std::copy(std::istream_iterator<unsigned char>(is), std::istream_iterator<unsigned char>(), std::back_inserter(fileContent));
std::cout << fileContent.size() << std::endl;

is.clear();
is.seekg(0);

std::string fileContent2;
std::copy(std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>(), std::back_inserter(fileContent2));
std::cout << fileContent2.size() << std::endl;

is.clear();
is.seekg(0);
std::vector<char> vecCHAR;
std::copy(std::istream_iterator<char>(is), std::istream_iterator<char>(), std::back_inserter(vecCHAR));
std::cout << vecCHAR.size() << std::endl;
return 0;

}

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2020-2021 一潭清泓
  • Powered by Hexo Theme Ayer
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信