您的位置:首页 > 移动开发 > IOS开发

ios::ate places the get-position pointer at the file end, enable tellg() to return the size of the f

2012-08-10 17:43 477 查看
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

void print_error(const char* p1, const char* p2 = 0);

int main(int argc, char* argv[])
{
if (3 != argc)
print_error("usage: function input_file output_file");

ifstream in(argv[1], ios::binary | ios::ate);
if (!in) print_error("cannot open input file", argv[1]);

long N = in.tellg();

char buffer
;

in.seekg(ios::beg);

ofstream out(argv[2], ios::binary);
if (!out) print_error("cannot open output file", argv[2]);

in.read(buffer, N);
out.write(buffer, N);

if (!in.good()) {
print_error("something strange happened");
exit(1);
}

in.close();
out.close();
return 0;
}

void print_error(const char* p1, const char* p2) {
if (p2)
cerr << p1 << ' ' << p2 << endl;
else
cerr << p1 << endl;
exit(1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐