Implementing a proper reading from file

I feel very dumb right now, so please, don't add up to this :) I'd PM one of the forum members, but he doesn't accept PM (Horscht).

I'll just give you full code in order to get a clear understanding of what the program does. So here you're looking at two functions: savelist() and loadlist(). Function savelist() works just fine, but I'm having troubles with implementing the loading system. I don't want to just print whatever is in the file, but to create a new list out of it. I guessed it works just like function addPassenger(), but the problem here is how to read it. So could you please take a look at this humiliation and give me an advice? Thank you!

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
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

#define PASSENGER_NAME 36
#define FLIGHTS_NUM 5
#define SEATS_NUM 24

struct Passenger {
	char name[PASSENGER_NAME];
	char flight[FLIGHTS_NUM];
	int seat_num;
	float price_paid;
	
	Passenger *next;
};

bool isEmpty(Passenger *head);
char menu();
void insertAsFirst(Passenger *&head, Passenger *&last, char *p_name, 
					char *p_flight, int p_seat_num, float p_price_paid);
void addPassenger(Passenger *&head, Passenger *&last, char *p_name, 
					char *p_flight, int p_seat_num, float p_price_paid);
void removePassenger(Passenger *&head, Passenger *&last);
void showlist(Passenger *current);
void savelist(Passenger *current);
void loadlist(Passenger *&head, Passenger *&last);


bool isEmpty(Passenger *head)
{
	if (head == NULL)
		return true;
		else return false;
}

char menu()
{
	char choice;
	printf("\nOption: ");
	scanf("%c", &choice);
	
	return choice;
}

void insertAsFirst(Passenger *&head, Passenger *&last, char *p_name, 
					char *p_flight, int p_seat_num, float p_price_paid)
{
	Passenger *temp = new Passenger;
	strcpy(temp->name, p_name);
	strcpy(temp->flight, p_flight);
	temp -> seat_num = p_seat_num;
	temp -> price_paid = p_price_paid;
	temp -> next = NULL;
	head = temp;
	last = temp;
}

void addPassenger(Passenger *&head, Passenger *&last, char *p_name, 
					char *p_flight, int p_seat_num, float p_price_paid)
{
	if(isEmpty(head))
		{
	insertAsFirst(head, last, p_name, p_flight, p_seat_num, p_price_paid);
	}
		else {
			Passenger *temp = new Passenger;
			strcpy(temp->name, p_name);
			strcpy(temp->flight, p_flight);
			temp -> seat_num = p_seat_num;
			temp -> price_paid = p_price_paid;	
			temp -> next = NULL;
			last -> next = temp;
			last = temp;
		}
}

void removePassenger(Passenger *&head, Passenger *&last)
{
	if(isEmpty(head))
		printf("Error: The list is already empty!\n");
		else if (head == last)
		{
			delete head;
			head = NULL;
			last = NULL;
		}
		
		else 
		{
			Passenger *temp = head;
			head = head -> next;
			delete temp;
		}
}

void showlist(Passenger *current)
{
	if(isEmpty(current))
		printf("Error: Cannot display list because it is empty!\n");
		else 
			{
				printf("Passengers: \n");
				while(current != NULL)
				{
				 printf("\nName: %s\nFlight: %s\nSeat: %d\nPaid: %.2f\n", current -> name, current -> flight, 
																	current -> seat_num, current -> price_paid);
				 printf("\n");
				 current = current -> next;
				}
			}
}

void savelist(Passenger *current)
		{
			
			if(isEmpty(current))
				printf("Error: Cannot save data, the list is empty!\n");
			else 
			{
				ofstream pass_list;
				pass_list.open("passlist.txt");
				while(current != NULL)
				{
				 pass_list << current -> name << " " << current -> flight << " " 
									<< current -> seat_num << " " << current -> price_paid;
				 pass_list << "\n";
				 current = current -> next;
				}
				pass_list.close();
			}
			
		}

void loadlist(Passenger *&head, Passenger *&last)
{
			        char p_name[PASSENGER_NAME];
			        char p_flight[FLIGHTS_NUM];
				int p_seat_num;
				float p_price_paid;
				ifstream pass_list("passlist.txt");
				if (pass_list.is_open())
					{
				while (pass_list >> p_name >> p_flight >> p_seat_num >> p_price_paid)
				{

				Passenger *temp = new Passenger;
				strcpy(temp->name, p_name);
				strcpy(temp->flight, p_flight);
			        temp -> seat_num = p_seat_num;
				temp -> price_paid = p_price_paid;
				temp -> next = NULL;
				head = temp;
				last = temp;
	
				}
				pass_list.close();
					} else cout << "Error: Unable to open file!\n"; 
}

int main(int argc, char **argv)
{
			//Used for menu manipulations
			char choice;
			char name[PASSENGER_NAME];
			char flight[FLIGHTS_NUM];
			int seat_num = 0;
			float price_paid = 0;
	
			Passenger *head = NULL;
			Passenger *last = NULL;
	
	
			printf("Menu: \n1.Add a Passenger\n2.Remove a Passenger\n3.Show List of Passengers\n4.Save to file\n5.Load from file\n6.Exit\n");
	
			do {
			choice = menu();
			switch(choice)
				{
					case '1':  cout << "Name: ";
							   cin >> name;
							   cout << "\nFlight: ";
							   cin >> flight;
							   cout << "\nSN: ";
							   cin >> seat_num;
							   cout << "\nPaid: ";
							   cin >> price_paid;
							addPassenger(head, last, name, flight, seat_num, price_paid);
					break;
			case '2': removePassenger(head, last);
					break;
			case '3': showlist(head);
					break;
			case '4': savelist(head);
					break;
			case '5': loadlist(head, last);
					break;
			default: printf("\n");
		}
		
		
	} while (choice != '6');
	
	return 0;
}
Last edited on
And the thing is - it does work. Kind of. Barely. It reads only the last line. It does record seat_num and price_paid into the list and even displays it. Whoever saw this problem earlier - yeah, it's the thing with names and flights again + reading line by line. I tried a couple methods already, but it wouldn't work.
Oh wait, I fixed it. Just had to replace some things. Okay, now how do I read line by line and insert passengers as they go in the file, i.e. 1st, 2nd, 3rd... etc?
Up. Still not solved. The question is - how do I read the passlist.txt line by line? I tried different loops but they only reset the place where it reads the file - either from the beginning of the file or from its end.
Topic archived. No new replies allowed.