size while writing a structure

i was writinga structure to a file and i tried to calculate the size of the member varibles and then the size of the object of that structue and ifound out that the summation of size of differnt member variable is not equal to the size of the object of that structure
here is the code of a small program
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
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <ctime>
#include <sys/stat.h>

using namespace std;

int main(int argc, char *argv[])
{ struct FAT{
                     char name[20];
                     int size;
                     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
                     int no_blk;
                     int file_head;//offset of 1st block of THIS file
                     };
 FAT file_F;    

ofstream out("VFS11",ios::out | ios::binary);
if(!out){cout<<"cannot open file"<<endl;}
                
string FileN;
char name[20];
//ptr_char temp;
//char directory[];
//   Enter the file directory
cout <<"please Enter the  directory of the file to be added:\n";
cin >>FileN;
cout<<"please enter the name for the file"<<'\n';
cin>>name;
cout<<sizeof(name)<<endl;
ifstream inf(FileN.c_str(), ios::in | ios::binary);//an input stream for the file to added
if(!inf) {cout << "Cannot open file.";}
struct stat fileInfo;
stat(FileN.c_str(), &fileInfo);
// file parameters
file_F .size=fileInfo.st_size;//size of file in bytes
file_F. device=(char)(fileInfo.st_dev + 'A');//this is the file drive
//string cre,mod;
file_F.CreDate=std::ctime(&fileInfo.st_ctime);//creation time
file_F.ModDate=std::ctime(&fileInfo.st_mtime);//last modification date
file_F.no_blk =file_F.size/1024;//this is  the no of blocks needed
file_F.file_head=7;
out.write(reinterpret_cast<char*>(& file_F),sizeof(file_F));
out.close();
inf.read(reinterpret_cast<char*>(& file_F),sizeof(& file_F));
inf.close(); 
cout<<file_F.file_head<<endl;
cout<<"the size of the object of structure is "<<sizeof(file_F)<<endl;
cout<<"the size of the variable no_blk is "<<sizeof(file_F.no_blk)<<endl;
cout<<"the size of the variable file)head is "<<sizeof(file_F.file_head)<<endl;
cout<<"the size of the variable is "<<sizeof(file_F.name)<<endl;
cout<<"the size of the variable name is "<<sizeof(file_F.size)<<endl;
cout<<"the size of the variable device is "<<sizeof(file_F.device)<<endl;
cout<<"the size of the variable CreDate is "<<sizeof(file_F.CreDate)<<endl;
cout<<"the size of the variable ModDate is "<<sizeof(file_F.ModDate)<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

and it gives o/p:
please Enter the  directory of the file to be added:
KurtCobain.jpg
please enter the name for the file
nirvana
20
7
the size of the object of structure is 44
the size of the variable no_blk is 4
the size of the variable file)head is 4
the size of the variable is 20
the size of the variable name is 4
the size of the variable device is 1
the size of the variable CreDate is 4
the size of the variable ModDate is 4
Press any key to continue . . .


so the object of structure is 44bytes while the summation of member variables sizes is 4+4+20+4+1+4+4=41 bytes
Probably data structure padding.
Compilers often pad to 4-byte boudaries in order to make memory accesses quicker.

That's what's happening here.
@Zhuge (730) and Disch (2624)
any more detailed explanation please.
and i also want to write several objects of that structure so while reading or writing from or to the file using seekg and seekp should i make an offset by 44
or 41
in other words is every time i write that structure it would be same size(44 bytes)
Last edited on
http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding

Use sizeof(FAT); (44), unless you are manually packing the data when you write it.
Topic archived. No new replies allowed.