number of records in binary file is in decimal

Hi. My size of binary file is 1920 KB and my struct size is 124 kb. now to find number of records in file I divided 1920/124 and it gives me 15.4.... do I add 1 to 15.4 and make it 16 or do i take it as 15?
I hope you are dealing with exact values here, otherwise this won't work.

But otherwise, think for a second. If you took it as 16 records, you would need 16×124KB=1984KB of space at least, while the file is only 1920KB.
I am reading with 15 as total records but I am getting garbage values as output when I read in to struct pointer

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
customer* getRecords(customer* cust, char file[], ifstream& Read, int tot_rec){


    Read.open(file, ios::in | ios::binary);

    if(!Read){
        cout<<"read fail"<<endl;
        return NULL;
        }

    for(int i=0;i<tot_rec;i++){

        Read.read((char*)&cust[i], sizeof(customer));
        cout<<cust[i].id<<endl;/////
        //read.getline(cust[i].fname, name);
        cout<<cust[i].fname<<endl;/////
        //read.getline(cust[i].lname, name);
        cout<<cust[i].lname<<endl;
        //read.getline(cust[i].contactNum, number);
        cout<<cust[i].contactNum<<endl;

    }
    Read.close();

    return cust;

}


How did you write it in the first place?
It looks like your structs are aligned by 128kb boundary.
(Did I read it right? YOur struct size is 124 kilobytes?)
Last edited on
its 124 bytes
So your binary is 1920 bytes.

Show how this file was created.
its already given by my tutor...
It looks like there was another 4 bytes of padding inside of structure.

I am getting garbage values as output when I read in to struct pointer
Do the first structure go out right?
nope, just gives out first ID or first fname ..rest all garbage values
Last edited on
fixed it..thanks...damn null character
Topic archived. No new replies allowed.