new struct record in binary file per function call

I'm trying to store some information in a binary file. However, i met with a problem that i can't rectify.

For example, if the first order comes in, there is a struct of information that includes orderNo 1, which will be stored into the binary file.

When there's a second order comes in, the information will be stored along with orderNo 2 into the binary file.

Here is my struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

struct adminInfo
{
	char foodName[MAX];
	int amt;
	double price;
	char foodInfo[MAX];
};

struct custInfo
{
	char custName[MAX];
	int tableNo;
	int orderNo;
	adminInfo ai;
	double total;
};


Here is my placeOrder.
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
void placeOrder(fstream& afile, const char fileName[], fstream& afile2, const char fileName2[])
{	 
	char custName[MAX];
	char foodInfo[MAX];
	int itemNo;
	bool yes = true;
	char yesno;
	bool repeat = true;
	static int orderNo = 1;
	
	displayOrderMenu(afile2, "Menu.dat");
	
	afile2.close();
	

	afile.open(fileName, ios::out | ios::app | ios::binary); // transaction
	afile2.open(fileName2, ios::in | ios:: out | ios::binary);

	cout << "Please place your order\n" << endl;
	
	custInfo ci;
	adminInfo ai;
	ci.total = 0;
			
    cin.clear();
	cin.ignore(MAX, '\n');
	
	cout << "May i have your name?: ";
    cin.getline(custName, MAX);
	strcpy(ci.custName, custName);
	
	cout << "Your table no: ";
	cin >> ci.tableNo;
	
	cout << endl;

	int amount;
	
	while(yes)
	{	 	 
	    ci.orderNo = orderNo;
	
		cin.clear();
		cin.ignore(MAX, '\n');
				
		cout << "Item No: ";
		cin >> itemNo;
		

		afile2.seekg((itemNo - 1) * sizeof(ai), ios::beg);
		afile2.read(reinterpret_cast <char *>(&ai), sizeof (ai));	   	  
		
		strcpy(ci.ai.foodName, ai.foodName);
						
		cout << "How many: ";
		cin >> ci.ai.amt;
		
		cin.clear();
		cin.ignore(MAX, '\n');
 		
		ai.amt = ai.amt - ci.ai.amt;
	
		afile2.seekp ((itemNo - 1) * sizeof (ai), ios::beg);
		afile2.write (reinterpret_cast<const char *>(&ai), sizeof(ai));
			
		ci.ai.price = ai.price * ci.ai.amt;
		ci.total = ci.ai.price + ci.total;  	
	
		cout << "Any instructions: ";
		cin.getline(foodInfo, MAX);
		strcpy(ci.ai.foodInfo, foodInfo);	
										
		afile.write(reinterpret_cast <const char*>(&ci), sizeof (ci));	  	  	 
		
		cout <<  "Any more items (Y/N) : " << yesno;
		
		cin >> yesno;
		
		cout << endl;
			
		if(yesno == 'n')
		{
			yes = false;
		}
			
		else if (yesno == 'y')
		{
			yes = true;
			yesno = NULL;
		}  	   	     	 	 
	}
	
	afile.close();
	afile2.close();
Last edited on
You will have to seek to end of file.

see seekp http://www.cplusplus.com/reference/ostream/ostream/seekp/

do a seekp(0, ios_base::end) before writing next record
i tried doing

 
afile.seekp(0, ios_base::end)


just before the

 
afile.write(reinterpret_cast <const char*>(&ci), sizeof (ci)); 


but it doesnt seems to work
One obvious problem, is that at line 3 you're using the assignment operator (=), not the comparison operator (==).

You haven't shown the declaration of ci, so it is impossible to tell if you're trying to write complex data types (such as std::string).

@codewalker - OP has opened the file for append. It should not be necessary to seek to eof.

@AbstractionAnon; true I missed that the first time.
To add to the comments while(condition = true) is going to create an infinite loop.

Solving this is still not going to solve the
the second order will be combined together with the first order despite appending my files.
problem.

@amoureux; posting mode code, at least full code for placeOrder and the structure of ci, will help forum members to answer
I have edited the question according to your feedbacks.
Topic archived. No new replies allowed.