Reading binary file with fstream

Aug 12, 2015 at 3:27pm
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 Aug 13, 2015 at 7:05am
Aug 13, 2015 at 7:05am
Hello? Anyone?
Aug 13, 2015 at 7:22am
Post minimal compiling example and example of file you are trying to read.
Aug 13, 2015 at 8:45am
Hey thanks for the answer but you're either blind or trolling. There's both code/file/output in my post.
Aug 13, 2015 at 8:59am
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.
Aug 13, 2015 at 10:22am
Sigh... Apparently i was using argv[0] instead of argv[1]... Changing that fixed the problem. Thanks for the help.
Aug 13, 2015 at 4:22pm
@AcarX:

I think you owe MiiNiPaa an apology for your comments, don't you?
Aug 13, 2015 at 4:31pm
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.