hi everbody,
i;ve made a file of size 128MB and fill it with nulls except for a part whose offset is (655424) i fill it with integer numbers from 1 to 129407 then i try to read an object of structure from it where that structure consists of four integer variables, a char variable , two string variables , and a char of array variable
when i try to read from that part of file using reinterpret_cast<> and an object of that structure i got the first integer right every time while the other integers are always fixed with a wrong values. then i try to check them by reading in ordinary way(using seekg then read) and i got them right so is there any mistake that i made or is it reinterpret_cast<> i know that it made data structure padding(Compilers often pad to 4-byte boudaries in order to make memory accesses quicker. so here the size of object of that structure is 48 rather than 45)is this may be the problem.
here is the code
#include <fstream>
#include <ctime>
#include <sys/stat.h>
using namespace std;
int main(int argc, char *argv[])
{
struct FAT{ int file_head;//offset of 1st block of THIS file
int no_blk;
int size;
int nrB;
char name[20];
char device;//this refers to original location on hard(is this benificial)
string CreDate;//this refers to creation date
string ModDate;// this refers to last time this file has changed
};
FAT file_F;
int h;
ifstream in("VFS88",ios::in | ios::binary);
if(!in){cout<<"cannot open file"<<endl;}
in.seekg(655424,ios::beg);
in.read(reinterpret_cast<char*>(& file_F),sizeof(& file_F));
in.close();
cout<<"the first offset is "<<file_F.file_head<<endl;
cout<<"the number of blocks is "<<file_F.no_blk<<endl;
cout<<"the size of that file is "<<file_F.size<<endl;
in.open("VFS",ios::in|ios::binary);
in.seekg(655424,ios::beg);
in.read((char *)&h, sizeof h);
cout<<"the correct 1st integer is "<<h<<endl;
in.read((char *)&h, sizeof h);
cout<<"the correct 2nd integer is "<<h<<endl;
in.read((char *)&h, sizeof h);
cout<<"the correct 3rd integer is "<<h<<endl;
in.read((char *)&h, sizeof h);
cout<<"the correct 4th integer is "<<h<<endl;
in.close();
system("PAUSE");
return EXIT_SUCCESS;
}
and the output is :
the first offset is 1
the number of blocks is 2293528
the size of that file is 8
the correct 1st integer is 1
the correct 2nd integer is 2
the correct 3rd integer is 3
the correct 4th integer is 4
Press any key to continue ...