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 ...

Edited 6 Years Ago by vlad44: mistake in the title

@Ancient dragon:
thanks for your reply it works
but I've another problem concerning the same matter I continue on the same file whose size is 128MB filled with nulls then I fill a part 64 bytes far from the beginning with the structure mentioned above{struct FAT} and I write several objects from it successively. As a check I made a function to check on the contents of that part using an object of that file and reinterpret_cast<> for reading. It works well but individually when I try to get the contents of the 3 objects one after another using a simple while loop similar to the one that i used to fill in that part it would continue for just one second then the console break down .
so I say it might be not that big deal and filed it with the same 3 objects individually with the check, but when I try to check them from another program (external program) which only open that file for reading and tries to read an object it did not work at all just a beebing from the case then the console break down very fast althougth it doesn't give any error either.
here is the code for the external program :

#include <cstdlib>
#include <iostream>
#include <fstream>\
#include <cstring>
#include <cstdio>
#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 f16;
ifstream ing("VFS",ios::in | ios::binary);
if(!ing){cout<<"cannot open file "<<endl;}
ing.seekg(64, ios::beg);
ing.read(reinterpret_cast<char*>(& f16),sizeof( f16));
        ing.close();
cout<<"the file name is "<<f16.name<<endl;
cout<<"the size of that file is "<<f16.size<<endl;
cout<<"this file was created in "<<f16.CreDate<<endl;
//cout<<"this file name is "<<f4.name<<endl;
cout<<"the first offset is "<<f16.file_head<<endl;  
cout<<"it was modified at "<<f16.ModDate<<endl;
cout<<"it was found at drive "<<f16.device<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

@Ancient dragon:
thanks for your reply it works
but I've another problem concerning the same matter I continue on the same file whose size is 128MB filled with nulls then I fill a part 64 bytes far from the beginning with the structure mentioned above{struct FAT} and I write several objects from it successively. As a check I made a function to check on the contents of that part using an object of that file and reinterpret_cast<> for reading. It works well but individually when I try to get the contents of the 3 objects one after another using a simple while loop similar to the one that i used to fill in that part it would continue for just one second then the console break down .
so I say it might be not that big deal and filed it with the same 3 objects individually with the check, but when I try to check them from another program (external program) which only open that file for reading and tries to read an object it did not work at all just a beebing from the case then the console break down very fast althougth it doesn't give any error either.
here is the code for the external program :

sorry for being too long
#include <cstdlib>
#include <iostream>
#include <fstream>\
#include <cstring>
#include <cstdio>
#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 f16;
ifstream ing("VFS",ios::in | ios::binary);
if(!ing){cout<<"cannot open file "<<endl;}
ing.seekg(64, ios::beg);
ing.read(reinterpret_cast<char*>(& f16),sizeof( f16));
        ing.close();
cout<<"the file name is "<<f16.name<<endl;
cout<<"the size of that file is "<<f16.size<<endl;
cout<<"this file was created in "<<f16.CreDate<<endl;
//cout<<"this file name is "<<f4.name<<endl;
cout<<"the first offset is "<<f16.file_head<<endl;  
cout<<"it was modified at "<<f16.ModDate<<endl;
cout<<"it was found at drive "<<f16.device<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

you can not write and read binary files with structure members that are std::string. why? One reason is that sizeof(std::string) does not include the actual characters but just the size of the c++ class. The sizeof(std::string) is the same regardless of how many characters are managed by that class.

Replace the std::string with character arrays and it should work.

@Ancient dragon
but how when i try every object by itself it works?
I used the sys/stat library to get information about files and the creation date and modification date are turned into string using a code like that

cout<<"Enter file path :";
    cin>>path;
struct stat fileInfo;
         stat(path.c_str(), &fileInfo);
string creF=std::ctime(&fileInfo.st_ctime);//creation time
         string modF=std::ctime(&fileInfo.st_mtime);//last modification date

so in case that this is the problem ain't there any way to convert a string to a char array ????
thanks for ur great help

This article has been dead for over six months. Start a new discussion instead.