Reading binary file with fstream

Hi there so here's code:

1
2
3
4
5
6
7
8
	std::streampos fsize = 0;
	fsize = f.tellg();
	f.seekg(0, 2);
	DWORD pDataLength = f.tellg() - fsize;
	f.seekg(0, 0);
	BYTE * data = new BYTE[pDataLength];
	f >> std::noskipws;
	f.read((char*)data, pDataLength);


It reads some stuff but it's not the result i expect. Here's the original hex file:
 
db 4b 16

The application reads something like:
 
4d xx xx (x is just placeholder)

I've tried bunch of stuff i read on internet but still same results.
Last edited on
Hello? Anyone?
Post minimal compiling example and example of file you are trying to read.
Hey thanks for the answer but you're either blind or trolling. There's both code/file/output in my post.
but you're either blind or trolling
Same can be said about you:
MiiNiPaa wrote:
Post minimal compiling example
Proof that it is not compiling: http://coliru.stacked-crooked.com/a/b189a66b6beaeca6

Your code looks fine (aside from using raw walues insted of named constants in seekg, but if you are using GCC 4.9.x those values are correct).
So potential possibilities are:
* There is a problem in your code either before or after, therefore I need complete compiling example which actually has a problem to actually test it and check what the problem might be.
* There is a problem in input file: easy to check, just need to see file itself. Doubles as input to test (1).
* Something other. To quickly eliminate most potential problems: use beg/end constants in seekg, check stream state before each operation (if failbit is set operation will fail), and check gcount after read to ensure that characters were extracted.
Sigh... Apparently i was using argv[0] instead of argv[1]... Changing that fixed the problem. Thanks for the help.
@AcarX:

I think you owe MiiNiPaa an apology for your comments, don't you?
I'm partially right, partially wrong. Even the code is mostly understandable only constructor for "f" is missing. But whatever makes you happy i'm sorry.
Topic archived. No new replies allowed.