A Broken File Reader

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?
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
34
35
36
37
38
39
40
41
42
43
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 = new char[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;

}
Topic archived. No new replies allowed.