read binary dat in class

closed account (iN8poG1T)
HI!! i want to ask if anyone knows how can i link my main.cpp to storeItem.cpp. the program can compile, but when i want to display the data, the data is empty. i only can enter the data, when i try to display, its empty, same goes to the other function, delete, search. i think theres something wrong with my storeitem.cpp?

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
main.cpp

#include "storeItem.h"
#include<fstream>
#include<conio.h>
#include<string.h>
#include<iostream>


using namespace std;


string item_ID,item_Name, item_Des, category, manufact;
int unit_store, unit_sold, year, month, day;
float sell_price, cost_price;

void enter_item()
{

        cout << " Enter The Item ID                         : ";
        cin >> item_ID;
        cin.ignore();
        cout << " Enter Item Name                           : ";
		getline(cin, item_Name);
        cout << " Enter Item Description                    : ";
		getline(cin, item_Des);
        cout << " Enter Item Category                       : ";
		getline(cin, category);
        cout << " Enter Item Manufacturer                   : ";
		getline (cin,manufact);
        cout << " Enter Item Selling Price                  : ";
        cin >> sell_price;
        cout << " Enter Item Cost Price                     : ";
        cin >>cost_price;
        cout << " Enter Item Unit in Store                  : ";
        cin >> unit_store;
        cout << " Enter Item Unit Sold                      : ";
        cin >> unit_sold;
        cout << " Enter Item Year of Date First Introduced  : ";
        cin >> year;
        cout << " Enter Item Month of Date First Introduced : ";
        cin >> month;
        cout << " Enter Item Day of Date First Introduced   : ";
        cin >> day;
}

void show_item()
{

        cout << " Item ID                        : "<<item_ID<< endl;
        cout << " Item Name                      : "<<item_Name <<endl;
        cout << " Item_Des                       : "<<item_Des<<endl;
        cout << " Category                       : "<<category<<endl;
        cout << " Manufacture                    : "<< manufact<<endl;
        cout << " Selling Price                  : "<<sell_price<<endl;
        cout << " Cost Price                     : "<<cost_price<<endl;
        cout << " Units in Store                 : "<< unit_store<<endl;
        cout << " Units Sold                     : "<< unit_sold<<endl;
        cout << " Year of Date First Introduced  : "<<year<<endl;
        cout << " Month of Date First Introduced : "<<month<<endl;
        cout << " Day of Date First Introduced   : "<<day<<endl;
}

void save_item()
{
    storeItem product;
    ofstream outFile;
    outFile.open("inventory.dat", ios :: binary | ios ::app);
    product.enter_item();
    outFile.write(reinterpret_cast<char*> (&product), sizeof(storeItem));
    outFile.close();
    cout << " Item Entered";
    cin.ignore();
    cin.get();
}

void display_all()
{
    storeItem product;
    ifstream inFile;
    inFile.open("inventory.dat", ios :: binary);
    if(!inFile)
    {
        cout << " Error Opening File";
        cin.ignore();
        cin.get();
        return;
    }
    cout<< "\n\n\n\t\tItem Details\n\n";
    while (inFile.read(reinterpret_cast<char*>(&product), sizeof(product)))
    {
        product.show_item();
    }
    inFile.close();
    cin.ignore();
    cin.get();

}

void search_item (string id)
{
    storeItem product;
    ifstream inFile;
    inFile.open("inventory.dat", ios :: binary);
    if (!inFile)
    {
        cout << " Error Opening File";
        cin.ignore();
        cin.get();
        return;
    }
    bool flag=false;
    while(inFile.read(reinterpret_cast<char*>(&product),sizeof(storeItem)))
          {
              if(product.getID()==id)
            {
                product.show_item();
                flag=true;
            }
          }
    inFile.close();
    if(flag==false)
        cout<< " Record Does Not Exist";
    cin.ignore();
    cin.get();
}

void update_item(string id)
{
    bool found = false;
    storeItem product;
    fstream File;
    File.open("inventory.dat",ios::binary|ios::in|ios::out);
    if(!File)
    {
        cout << " File could not be open";
        cin.ignore();
        cin.get();
        return;
    }
    while (!File.eof()&&found == false)
    {
        File.read(reinterpret_cast<char*>(&product), sizeof(storeItem));
        if(product.getID()==id)
        {
            product.show_item();
            cout << " Enter New Details";
            product.enter_item();
            int pos=(-1)*static_cast<int>(sizeof(product));
            File.seekp(pos, ios ::cur);
            File.write(reinterpret_cast<char*>(&product), sizeof(storeItem));
            cout << " Details Update";
            found= true;
        }
    }
    File.close();
    if(found==false)
        cout << " Record not found";
    cin.ignore();
    cin.get();
}

void delete_item(string id)
{
    storeItem product;
    ifstream inFile;
    inFile.open("inventory.dat", ios :: binary);
    if(!inFile)
    {
        cout << " Error opening file";
        cin.ignore();
        cin.get();
        return;
    }
    ofstream outFile;
    outFile.open("Temp.dat", ios :: out);
    inFile.seekg(0,ios::beg);
    while(inFile.read(reinterpret_cast<char*>(&product), sizeof(storeItem)))
    {
        if(product.getID()!=id)
        {
            outFile.write(reinterpret_cast<char*>(&product), sizeof(storeItem));

        }
    }
    outFile.close();
    inFile.close();
    remove("inventory.dat");
    rename("Temp.dat", "inventory.dat");
    cout<< " Record Deleted";
    cin.ignore();
    cin.get();
}

int main ()

{
    string id;
	int option;


	do
    {
        cout<<"\n\n\t\t**** Good Morning and Welcome ****";
        cout<<"\n\n\n\t\t\tMAIN MENU\n\t\t=====================\n\t\t";
        cout<<"[1] Insert New Item\n\t\t";
        cout<<"[2] Update Item\n\t\t";
        cout<<"[3] Delete Item\n\t\t";
        cout<<"[4] Display Item\n\t\t";
        cout<<"[5] Search Item\n\t\t";
        cout<<"[6] Exit\n\t\t";
        cout<<"\n\t\t=====================\n\t\t";
        cout<<"Enter Your Choice : ";
        cin >> option;
        system ("cls");

	switch (option)
	{
	    case 1: enter_item();
	    break;
	    case 2: update_item(id);
	    break;
        case 3: delete_item(id);
	    break;
        case 4: display_all();
	    break;
	    case 5: search_item (id);
	    break;
	    case 6: save_item();
	    break;
        case 7:
           cout<<"\n\n\t\tThank you ";
    		exit(12);
    			break;

	    default :
            cout << " Invalid option"<<endl;

	}
    }while (option);

    system ("pause");
    system("cls");
getch();
return 0;

}


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
storeItem.cpp

#include "storeItem.h"
#include<fstream>
#include<string.h>
#include<conio.h>
#include<iostream>

using namespace std;

storeItem :: storeItem(){}
storeItem :: storeItem(string item_ID, string item_Name, string item_Des, string category,string manufact,int unit_store,int unit_sold,int year,int month,int day, double sell_price,double cost_price){}
void storeItem :: enter_item(){}
void storeItem :: show_item () const{}
void storeItem :: save_item(){}
void storeItem :: display_all(){}
void storeItem:: search_item(){}
void storeItem :: update_item (){}
void storeItem :: delete_item(){}
string storeItem :: getID()// function to use for all function
{
    return item_ID;
}
string storeItem :: getName()
{
    return item_Name;
}

string storeItem :: getDes()
{
    return item_Des;
}
string storeItem :: getManufact ()
{
    return manufact;
}
string storeItem :: getCategory ()
{
    return category;
}

double storeItem :: getSellPrice()
{
    return sell_price;
}
double storeItem::getCostPrice()
{
    return cost_price;
}

int storeItem :: getUnit()// function to use for all function
{
    return unit_store;
}
int storeItem :: getSold()// function to use for all function
{
    return unit_sold;
}
int storeItem ::  getYear()// function to use for all function
{
    return year;
}
int storeItem :: getMonth()// function to use for all function
{
    return month;
}

int storeItem :: getDay()// function to use for all function
{
    return day;
}

closed account (iN8poG1T)
This is my header file, Im sorry i cannot put them together because it is too long hence, i put here

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

storeitem.h

#ifndef STOREITEM_H_INCLUDED
#define STOREITEM_H_INCLUDED


#include<fstream>
#include<string.h>
#include<conio.h>
#include<iostream>

using namespace std;

class storeItem
{
private :

     //variables declaration
    string item_ID, item_Name, item_Des, category, manufact;
    int unit_store, unit_sold, year, month, day;
    double sell_price, cost_price;


public :
    //to access member function
    storeItem();
    storeItem(string, string, string, string, string, int, int, int, int, int, double, double);
    void enter_item();
    void show_item()const;
    void save_item();
    void display_all();
    void search_item();
    void update_item ();
    void delete_item();

	string getID();// function to use for all function
    string getName();
    string getDes();
    string getCategory ();
    string getManufact ();
    double getSellPrice();
    double getCostPrice();
	int getUnit();// function to use for all function
	int getSold();// function to use for all function
	int getYear();// function to use for all function
	int getMonth();// function to use for all function
	int getDay();// function to use for all function


};

#endif // STOREITEM_H_INCLUDED
the program can compile, but when i want to display the data, the data is empty. i only can enter the data, when i try to display, its empty, same goes to the other function, delete, search. i think theres something wrong with my storeitem.cpp?

Your classes contain non-trivially constructed classes, such as std::string, and therefore you can't use the binary write()/read() functions to directly write and read the whole class. You will need to write() each individual data member to the file and you will also need to somehow store the string lengths in the file so that you can first retrieve the size before you retrieve the string. Remember every std::string can have different lengths and the binary write()/read() functions work with fixed lengths.

closed account (iN8poG1T)
I'm sorry I still don't get it what do you mean?
What do you not understand?

Do you understand that you can't write a std::string directly to a file using the write() function?

Do you understand that in order to read() something from a file that you must know how many bytes need to be read.?

Do you understand that you must read() the exact number of bytes that you wrote with write()?


Topic archived. No new replies allowed.