Won't write to file

This seems like it should work. I've ran my program and added a customer. At the end, she appears on the screen just fine but she's not appearing in my output file. I'm probably making a very simple error but I'm at a loss as to what is happening.

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
 #include <iostream>
#include <string>
#include <list>
#include <iomanip>
#include <fstream>
#include<queue>
#include "customers.h"
#include "date.h"
#include "items.h"
#include "bids.h"
using namespace std;

void createItemList(fstream&, list<Item> &);
void createCustomerList(fstream&, list<Customer> &);
void createBidQueue(fstream&, list<Item> &, list<Item>::iterator &);

void findCustomer(int cid, list<Customer> &, list<Customer>::iterator &, bool &); 
void findItem(int id, list<Item> &, list<Item>::iterator &, bool &); 

void enterNewCustomer(list<Customer> &, list<Customer>::iterator &);
void enterNewItem(list<Item> &, list<Item>::iterator &, list<Customer> &, list<Customer>::iterator &);
void enterNewBid(list<Item> &, list<Item>::iterator &, list<Customer> &, list<Customer>::iterator &);

void writeCustomerList(fstream&, list<Customer> &, list<Customer>::iterator &);

void enterCustomerID(int &);
void enterTime(int &, int &, int &, int &, int &, int &, int &, bool &);
void displayMenu();

const double COM_PER = 0.1;
double Item::totalCommission = 0;

int main()
{
	list <Item> itemList;
	list <Item>::iterator i;
	list <Customer> customerList;
	list <Customer>::iterator c;
	list <Bid> bidList;
	list <Bid>::iterator b;
	char ch;
	int choice, cid, yy, mm, dd, hh, min, ap, t=0;          
	bool invDate=false;


    fstream itemfile, custfile, bidfile;

	 //open the customers file
    custfile.open("customers.txt", ios::in | ios::out);
    if (!custfile)
    {
        cout << "The customer file does not exist. "
             << "This program will terminate." << endl;
        return 1;
    }
//code inbetween

    //create the Customer list
    createCustomerList(custfile, customerList);
	for (c=customerList.begin(); c!=customerList.end(); ++c)
	{
		c->printCustomer();
		c->writeCustomer(custfile);
	}

	custfile.close();
    itemfile.close();
	bidfile.close();
	system("pause");
	return 0;
}

void Customer::writeCustomer(fstream &cfile)
	{
		cfile << customerID << endl;
		cfile << firstName << endl;
		cfile << lastName << endl;
		cfile << email << endl;
	}


******** dBay ********
** Bidding Software **

Select one of the following:
1: Register a new customer.
2: Add new item for sale.
3: Enter a bid for an item.
4: End bidding for an item.
5: Print a customer's items for sale.
6: To print a list of all items for sale.
7: To print a list of all items sold.
8: To print a list of items and their bids.
9: To exit
Enter your choice: 1


Enter new Customer ID: 8
Enter First Name: Cass
Enter Last Name: Elliot
Enter email address: ce@dkjs.com
Select one of the following:
1: Register a new customer.
2: Add new item for sale.
3: Enter a bid for an item.
4: End bidding for an item.
5: Print a customer's items for sale.
6: To print a list of all items for sale.
7: To print a list of all items sold.
8: To print a list of items and their bids.
9: To exit
Enter your choice: 9

//Entered Cass Elliot and it shows that she has been entered

Customer ID: 1
Customer Name: John Smith
Email Address: johnsmith@aol.com
Customer ID: 2
Customer Name: Jane Doe
Email Address: jdoe@frontier.com
Customer ID: 3
Customer Name: Mark Goose
Email Address: mg@charter.net
Customer ID: 5
Customer Name: Meg Ryan
Email Address: megryan@aol.com
Customer ID: 6
Customer Name: Jean Moss
Email Address: jmoss@frontier.com
Customer ID: 8
Customer Name: Cass Elliot
Email Address: ce@dkjs.com
Press any key to continue . . .

Customers file is unchanged:

1
John
Smith
johnsmith@aol.com
2
Jane
Doe
jdoe@frontier.com
3
Mark
Goose
mg@charter.net
5
Meg
Ryan
megryan@aol.com
6
Jean
Moss
jmoss@frontier.com
if you are using fstreams, not that you need to use seekx() functions each time you are changing between reading/writing
Last edited on
I added the seekp() and it made no difference.

1
2
3
4
5
6
	custfile.seekp(ios::beg);
	for (c=customerList.begin(); c!=customerList.end(); ++c)
	{
		c->printCustomer();
		c->writeCustomer(custfile);
	}
On line 56 you indicate there is code that was snipped. Does this code happen to read to the end of custfile? And, if it does (or createCustomerList does,) do you clear the error flags before attempting to write to the file?
I forgot the function that reads the file. Here it is. It does read to the end of the file because it created objects for all of my customers and I was able to print them on screen. Jean Moss was the last record and I added Cass Elliot while running the program. But I did not clear error flags. I'm not familiar with them. I'll do some research on that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void createCustomerList(fstream& cfile, list<Customer> &custlist)
{
	Customer ctemp;
	char ch;
	int cID;
	string fName, lName, email;

	cfile >> cID;

	while(cfile)
	{
		cfile.get(ch);
		getline(cfile, fName);
		getline(cfile, lName);
		getline(cfile, email);
		ctemp.setCustomer(cID, fName, lName, email);
		custlist.push_back(ctemp);
		cfile.clear();
		cfile >> cID;
	}
}
Topic archived. No new replies allowed.