fstream

My question is when i inside a thing to file but the file didnt update ? But it did display on code when i run it again.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include<string>
#include<fstream>
#include<iomanip>
#include<iostream>

using namespace std;

struct Database_struct
{
    int id;
    char name[100];
    int quant;
    double cost;
};
typedef struct Database_struct Database;
void write_to_binary_file(Database *p_Data);
void read_from_binary_file(Database *p_Data);
void print_records(Database *p_Data);

int main()
{
    int i,j;
    Database d;
    Database db[100],
    temp[8]={
        3,"Electric Sander",7,57.98,
        17,"Hammer",76,11.99,
        24,"Jig Saw",21,11.00,
        39,"Lawn Mover",3,79.50,
        56,"Power Saw",18,99.99,
        68,"Screw-driver",106,6.99,
        77,"Sledge Hammer",11,21.50,
        83,"Wrench",34,7.50
    };
    int n;
    ifstream myfile ("hardware.dat");
    if (myfile.is_open())
    {
        myfile.close();
        cout<<" Database is present, so loading:"<<endl;
        read_from_binary_file(db);
    }
    else
    {
        cout<<"Creating new Database :";
        for(i=0;i<100;i++)
        {
            for(j=0;j<8;j++)
            {
                if(temp[j].id == i)
                {
                    memcpy(&db[i],&temp[j],sizeof(Database));
                    break;
                }
            }
            if(j>=8)
            {
                memset(&db[i],0,sizeof(Database));
            }
        }

        write_to_binary_file(db);
    }
    while(1)
    {
        cout<<"\n1.Display\n2.Insert\n3.Update\n4.Delete\n5.Exit\n";
        cin>>n;
        switch(n)
        {
            case 1:
                print_records(db);
                break;
            case 2:
            again:
                cout<<"Enter the record number:";
                cin>>d.id;
                if(db[d.id].name[0]!=0)
                {
                    cout<<"\nThis id is present in Data Base, so enter other:\n";
                    goto again;
                }
                cout<<"Enter the name:";
                do
                {
                    cin.getline(d.name,sizeof(d.name));
                }while(strcmp(d.name,"")==0);
                cout<<"Enter the Quant:";
                cin>>d.quant;
                cout<<"Enter th cost:";
                cin>>d.cost;
                memcpy(&db[d.id],&d,sizeof(Database));
                write_to_binary_file(db);
                break;
            case 3:
            again1:
                cout<<"Enter the record number:";
                cin>>d.id;
                if(db[d.id].name[0]==0)
                {
                    cout<<"\nThis id is empty so can't update, so enter other:\n";
                    goto again1;
                }
                cout<<"Enter the name:";
                do
                {
                    cin.getline(d.name,sizeof(d.name));
                }while(strcmp(d.name,"")==0);
                cout<<"Enter the Quant:";
                cin>>d.quant;
                cout<<"Enter th cost:";
                cin>>d.cost;
                memcpy(&db[d.id],&d,sizeof(Database));
                write_to_binary_file(db);
                break;
            case 4:
            again2:
                cout<<"Enter the record number:";
                cin>>d.id;
                if(db[d.id].name[0]==0)
                {
                    cout<<"\nThis id is empty so can't delete, so enter other:\n";
                    goto again2;
                }
                memset(&db[d.id],0,sizeof(Database));
                write_to_binary_file(db);
                break;
        }
        if(n==5)
            break;
        
}
}
void write_to_binary_file(Database *p_Data)
{
    int i;
    fstream binary_file("hardware.dat",ios::out|ios::binary);
    if(binary_file.is_open())
    {
        for(i=0;i<100;i++)
        {
            binary_file.write((char*)(&p_Data[i]),sizeof(Database));
        }
            binary_file.close();
        
        }else
        {
            cout<<"\nCan't open file";
        }
}
void read_from_binary_file(Database *p_Data)
{
        fstream binary_file("hardware.dat",ios::binary|ios::in);
        
        binary_file.read((char*)p_Data,sizeof(Database)*100);
        binary_file.close();
        
}
void print_records(Database *p_Data)
{
    int i;
        
    cout<<setw(20)<<"Record"<<setw(20)<<"Tool name"<<setw(20)<<"Quantity"<<setw(20)<<"Cost"<<endl;
    for(i=0;i<100;i++)
    {
        if(p_Data[i].name[0]!=0)
            cout<<setw(20)<<p_Data[i].id<<setw(20)<<p_Data[i].name<<setw(20)<<p_Data[i].quant<<setw(20)<<p_Data[i].cost<<endl;
    }
}

//print_records(db);
//cout<<endl; 


here is files

3 ElectricSander 7 57.98
17 Hammer 76 11.99
24 JigSaw 21 11.00
39 LawnMower 3 79.50
56 PowerSaw 18 99.99
68 Screwdriver 106 6.99
77 SledgeHammer 11 21.50 
83 Wrench 34 7.50
My question is when i inside a thing to file but the file didnt update ? But it did display on code when i run it again.
What makes you think that the data isn't written to the file?
By the way: What you print is the array. It exists independently from the file.

That you always print/write 100 isn't the best idea. That way you print/write data that is invalid.
What you mean?
What if you have only 80 items in db? You print 100. What is going to print for the last 20 since you're using 100 as your loop limit, not the number actually present.
when i try to hide a file to see if it read from file. When I do, it still same thing like it dont need file. I want to to read from file
Ok, I see that you set invalid records to 0 so that you don't show them.

What you have to do is this:
1
2
3
4
5
6
7
8
9
void read_from_binary_file(Database *p_Data)
{
        fstream binary_file("hardware.dat",ios::binary|ios::in);
        
        memset(p_Data,0,sizeof(Database)*100);
        binary_file.read((char*)p_Data,sizeof(Database)*100);
        binary_file.close();
        
}
OK I will try. Here is other problems. is I need to change the file from. Txt. To .bin
Last edited on
coder777:When i put that in there my complier say:Segmentation Fault (core dumped)
Topic archived. No new replies allowed.