This topic is now archived and is closed to further replies.


problem reading from a file =/

Recommended Posts

in an attempt to read some info from the header file of an exe (just for shits and giggles, and because i wanna learn more about the structure of an exe file) i seem to have come accross a problem in the way i read data from the file =/
#include <iostream>
#include <fstream>
#include <conio.h>

void main()
	std::fstream file("1.exe",std::ios::in|std::ios::binary);

	unsigned short ID;
	unsigned short bytesinlastpage;
	unsigned short numberofpages;
	unsigned short segmenttableentries;
	unsigned short headersize;
	unsigned short minimummemory;
	unsigned short maximummemory;
	unsigned short initialoffsetinparagraphs;
	unsigned short initialoffsetinbytes;
	unsigned short relocationtableoffset;
	unsigned short overlaynumber;


	std::cout << "ID: " << ID << std::endl;
	std::cout << "Number of Byts in Last Page: " << bytesinlastpage << std::endl;
	std::cout << "Number of Pages: " << numberofpages << std::endl;
	std::cout << "Segment Table Entries: " << segmenttableentries << std::endl;
	std::cout << "Header Size: " << headersize << std::endl;
	std::cout << "Minimum Required Memory: " << minimummemory << std::endl;
	std::cout << "Maximum Requested Memory: " << maximummemory << std::endl;
	std::cout << "Initial Offset in Paragraphs: " << initialoffsetinparagraphs << std::endl;
	std::cout << "Initial Offset in Bytes: " << initialoffsetinbytes << std::endl;
	std::cout << "Relocation Table Offset: " << relocationtableoffset << std::endl;
	std::cout << "Overlay Number: " << overlaynumber << std::endl;

now it works in that it reads data from the file, but i need to to read two bytes at a time so that the value of each two byte int that im interested in gets its value, from looking at the file in a hex editor and comparing the results shown it seems to be reading only ONE byte each time the get function is called. even tho i clearly specify i want to read the size of each variable (which i really could replace that with just 2, either way). is there something conceptualy that i am missing here? because from what ive read this should work... i dont understand why its not =( all i need it to do is just read 2 bytes at a time and instead of putting the data in a char* i need it in a unsigned short, which if is why i used the (char*)& so that itd read it as a char* like its supposed to and place it in the location of the unsigned short. Bungo!

Share this post

Link to post
Share on other sites