writing and reading a structure from/to file

iwas using reinterpret_cast<> to write and read structre but ihave found something that is really very wired
when i read from the file using reinterpret_cast<> it works well while either
in same program or in a second program
but when i try to access the file using seekg and then reading i face a problem
here is the code
i am trying to read from a file where i have write before an int 5 int 6 char a
who are the member variables of most structure written below thn i try to read them in this program once using reierprete_cast<> and another time using accessing to the file :

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
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{struct most{
       int s;
       int d;
       char f;
       };
    
    most q;
    ofstream out1("temp", ios::in | ios::out | ios::binary );
    if(!out1){cout<<"what is this \n";}
    ifstream in1("temp",ios::in | ios::binary);
    if(!in1){cout<<"what is this in o/p \n";}
    in1.seekg(0, ios::beg);
    in1.read(reinterpret_cast<char*>(& q),sizeof(q));
    in1.close();
    cout<<q.s<<" "<<q.d<<" "<<" "<<q.f<<" "<<endl;
    char k;
    in1.seekg(8,ios::beg);
    in1.read((char *)&k , sizeof k);
    cout<<k<<endl;


and the output is :
[output]
5 6 a
|
Press any key to continue . . .[/output
so what does this instruction made iknow that it make data structure padding
but what is the problem in this code?and by the way can i use that instruction to
while dealing with a structure having one of its parameters as an array
?????????
line 20: you close the file
line 24: you read from the same file
thank you. and sorry for such a trivial error.
Topic archived. No new replies allowed.