#### Archived

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

# problem reading from a file =/

This topic is 5283 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 minimummemory;
unsigned short maximummemory;
unsigned short initialoffsetinparagraphs;
unsigned short initialoffsetinbytes;
unsigned short relocationtableoffset;
unsigned short overlaynumber;

file.get((char*)&ID,sizeof(ID));
file.get((char*)&bytesinlastpage,sizeof(bytesinlastpage));
file.get((char*)&numberofpages,sizeof(numberofpages));
file.get((char*)&segmenttableentries,sizeof(segmenttableentries));
file.get((char*)&minimummemory,sizeof(minimummemory));
file.get((char*)&maximummemory,sizeof(maximummemory));
file.get((char*)&initialoffsetinparagraphs,sizeof(initialoffsetinparagraphs));
file.get((char*)&initialoffsetinbytes,sizeof(initialoffsetinbytes));
file.get((char*)&relocationtableoffset,sizeof(relocationtableoffset));
file.get((char*)&overlaynumber,sizeof(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 << "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;

getch();
exit(0);
}

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 on other sites
crap nevermind lol. i wanted read not get. im such a n00b.