Writing To Binary 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

struct myRecord
{
    char name[80];
    double balance;
    unsigned long account_num;
};

int main()
{
    struct myRecord acc;
    acc.balance = 1.3;
    acc.account_num = 34;

    ofstream outbal("balance", ios::out | ios::binary);
    if(!outbal)
    {
        cout<< "Cannon Open File,\n";
        return 1;
    }

    outbal.write((char *)&acc, sizeof(struct MyRecord));
    outbal.close();

    ifstream inbal("balance", ios::in | ios::binary);
    if(!inbal)
    {
        cout<<"Cannon Open File.\n";
        return 1;
    }
    inbal.read((char *)&acc,sizeof(struct MyRecord));

    cout<<acc.name<<endl;
    cout<<"\n Account # " << acc.account_num;
    cout.precision(2);
    cout.setf(ios::fixed);
    cout<<endl<<" Balance : £"<<acc.balance;

    outbal.close();

    return 0;
}


When trying to save compile and run the code it comes up with these errors...


In function 'int main()':|
|28|error: invalid application of 'sizeof' to incomplete type 'MyRecord' |
|37|error: invalid application of 'sizeof' to incomplete type 'MyRecord' |
||=== Build finished: 2 errors, 0 warnings ===|



Both Code::Blocks v10.05 and DevC++ v4.9.9.2 are throwing up this error...
Thanks in advance for any help... this code is ripped from the net, I am going to adapt it into my project once i can get this version working.
You are trying to get the size of MyRecord when your type is called myRecord.
Oh.. feel like a bit of an idiot now...

Also is it usual for it to display like this? :

http://imgur.com/IMKNM

EDIT:
as in the 'ú' and the other symbols...
Last edited on
That isn't normal at all
Topic archived. No new replies allowed.