I've been working on a file reading piece of software, that has a few major issues. It has a habit of putting data in places where it originally does not belong. It also cannot read binary files like exe's, xor data and so on.
So how do I fix this mess any ideas?
int getSize(string fname)
{
ifstream infile(fname, ios::in | ios::binary);
infile.seekg (0,infile.end);
long size = infile.tellg();
infile.seekg (0);
infile.close();
return size;
}
void readSystem(string fname,string outFileName)
{
size_t buffer_size = 1<<20;
char *buffer = newchar[buffer_size];
std::ifstream fin(fname);
ofstream outFile("OUT");
// Anti overwrite
string theNewOutFileName(outFileName);
if(getSize(outFileName) > 0)
{
cout << "Overwrite error" << endl;
exit(0);
}
while (fin)
{
// Try to read next chunk of data
fin.read(buffer, buffer_size);
// Get the number of bytes actually read
size_t count = fin.gcount();
// If nothing has been read, break
outFile << buffer;
if (!count)
break;
// Do whatever you need with first count bytes in the buffer
}
outFile.close();
fin.close();
delete[] buffer;
}