reading streams and nested structures
Nov 22, 2014 at 10:29pm UTC
I'm writing a program to read sections of the boot sector of a hard drive.
I'm fairly new to a few elements in this program so I have no doubt I did something dumb.
I have a structure that breaks the mbr apart, and one that breaks each partition apart. I am able to read from the mbr, but it appearently is hanging when I try to read from a partition. It compiles, but if you look at the function getPartition, the cout statement which prints "test2" doesn't print after the stream.read function. What am I doing wrong here?
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#include <iostream>
#include <fstream>
using namespace std;
typedef unsigned int uint;
typedef unsigned char uchar;
struct partition_t{
uchar status;
uchar firstHead;
uchar firstSector;
uchar firstCylinder;
uchar partitionType;
uchar lastHead;
uchar lastSector;
uchar lastCylinder;
uchar firstSectorLBA[4];
uchar numberOfSectors[4];
};
struct mbr_t{
bool fileAccess;
partition_t partition1;
partition_t partition2;
partition_t partition3;
partition_t partition4;
bool partition1Valid;
uchar bootSignature[2];
bool bootSignatureValid;
}mbr;
partition_t getPartition( ifstream &stream, uint partNum ){
partition_t output;
uint baseOffset;
switch ( partNum ){
case 1:
baseOffset = 446;
break ;
case 2:
baseOffset = 462;
break ;
case 3:
baseOffset = 478;
break ;
case 4:
baseOffset = 494;
break ;
}
if (stream.seekg( baseOffset ).good()){
/* Yes, I know, I'm being lazy with the error handling. I'll fix it later */
cout <<baseOffset<<endl;
cout <<"test" <<endl;
stream.read( reinterpret_cast <char *>( output.status ) ,1 );
stream.seekg( baseOffset + 1 );
cout <<"test2" <<endl;
stream.read( reinterpret_cast <char *>( output.firstHead ) ,1 );
/*add the rest of the partition stuff here*/
}
return output;
}
void printPartition( partition_t p ){
cout << "Status: " << std::hex << (uint)p.status <<endl;
cout << "Status: " << std::hex << (uint)p.firstHead <<endl;
}
mbr_t getBootSignature( istream &stream ){
mbr_t output;
if (stream.seekg( 510 ).good()){
if (!stream.read( reinterpret_cast <char *>( output.bootSignature ), 2 ).fail()){
if ( (mbr.bootSignature[0] != 0x55) || (mbr.bootSignature[1] != 0xaa) )
output.bootSignatureValid = false ;
}
else
output.bootSignatureValid = false ;
}
else
output.bootSignatureValid = false ;
return output;
}
void printBootSignature(mbr_t MBR){
cout << std::hex << (uint)MBR.bootSignature[0] << (uint)MBR.bootSignature[1] <<endl;
}
int main(int argc, char ** argv){
ifstream datafile ( argv[1] , ios::binary);
mbr_t mbr;
if (datafile.good()){
cout << "Reading Data...\n" ;
mbr = getBootSignature( datafile );
printBootSignature(mbr);
mbr.partition1 = getPartition( datafile, 1 );
printPartition( mbr.partition1 );
cout <<"test again" <<endl;
}
else mbr.fileAccess = false ;
return 0;
}
Topic archived. No new replies allowed.