#include <iostream>
#include <fstream>
int main()
{
int length;
char *buffer;
std::fstream rfile;
rfile.open("test.txt",std::fstream::in|std::fstream::binary);
if(!rfile)
{
std::cerr<<"Open test.txt failed!"<<std::endl;
return -1;
}
//get length of file:
rfile.seekg(0,std::ios::end);
length = rfile.tellg();
rfile.seekg(0,std::ios::beg);
std::cout<<"length:"<<length<<std::endl;
//allocate memory:
buffer = new char[length];
//read data as a block:
rfile.read(buffer,length);
rfile.close();
//write to outfile
std::fstream wfile("new.txt",std::fstream::out|std::fstream::binary);
wfile.write(buffer,length);
//delete buffer
delete[] buffer;
//close rfile wfile
rfile.close();
wfile.close();
return 0;
}
#include <fstream>
int main()
{
int length;
char *buffer;
std::fstream rfile;
rfile.open("test.txt",std::fstream::in|std::fstream::binary);
if(!rfile)
{
std::cerr<<"Open test.txt failed!"<<std::endl;
return -1;
}
//get length of file:
rfile.seekg(0,std::ios::end);
length = rfile.tellg();
rfile.seekg(0,std::ios::beg);
std::cout<<"length:"<<length<<std::endl;
//allocate memory:
buffer = new char[length];
//read data as a block:
rfile.read(buffer,length);
rfile.close();
//write to outfile
std::fstream wfile("new.txt",std::fstream::out|std::fstream::binary);
wfile.write(buffer,length);
//delete buffer
delete[] buffer;
//close rfile wfile
rfile.close();
wfile.close();
return 0;
}