Open File function

Hi all, I've just about finished that program (so can hopefully stop pestering you all with it soon). All my functions are written and working, but I need help with my open file function. I put my whole program code below.

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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>

using namespace std;

struct Person
{
	string firstName;
	string lastName;
	int age;
};

void getNames(vector <Person> &people);
void display(const vector <Person> &people);
void open(vector <Person> &people);
void save(ofstream &fout, vector <Person> people);
void remove(ofstream &fout, vector <Person> &people);
void sort(vector <Person> &people);			

int main()
{
	ofstream fout;
	vector <Person> people;
	cout << "Welcome to the Names Catalogue. Please start by opening the file." << endl;
	cout << endl;
	int sel;
	while (true)
	{
		cout << "--------------------------" << endl;
		cout << "What would you like to do?" << endl;
		cout << "--------------------------" << endl;
		cout << "Enter 1 add entries." << endl;
		cout << "Enter 2 to display entries." << endl;
		cout << "Enter 3 to save entries." << endl;
		cout << "Enter 4 to open file." << endl;
		cout << "Enter 5 to remove an entry." << endl;
		cout << "Enter 6 to sort by last namme." << endl;
		cout << "Enter 7 to quit." << endl;
		cout << "--------------------------" << endl;
		cout << "Enter selection: ";
		cin >> sel;
		cout << "--------------------------" << endl;
		switch (sel)
		{
			case 1: getNames(people); break;
			case 2: display(people); break;
			case 3: save(fout, people); break;
			case 4: open(people); break;
			case 5: remove(fout, people); break;
			case 6: sort(people); break;
			case 7: cout << "Goodbye."; break;
			default: cout << "Error: Incorrect input. Please try again." << endl; break;
		}
		if (sel == 7) break;
	}
	fout.close();
}

void getNames(vector <Person> &people)
{
	Person aPerson;
	while (aPerson.firstName != "quit")
	{
		cout << "Enter first name, last name, and age: ";
		cin >> aPerson.firstName;
		if(aPerson.firstName=="quit") break;
		cin >> aPerson.lastName >> aPerson.age;
		people.push_back(aPerson);
	}
}

void display(const vector <Person> &people)
{
	for (int i = 0; i < people.size(); i++)
	{
		cout << "Name: " << people[i].lastName << ", " << people[i].firstName << ". Age: " << people[i].age << endl;
	}
	cout << endl;
}

void open(vector <Person> &people)
{
	ifstream fin("names.txt");
	Person aPerson;
	string tmp;
	stringstream ss;
	
	if (fin.fail()) 
	{
		cout << "No file to open." << endl;
	}
	else cout << "File opened." << endl;
	
	while(!fin.eof())
	{
		getline(fin, aPerson.lastName,';');
		
		getline(fin, aPerson.firstName,';');
	
  		getline(fin, tmp); 
  		ss << tmp;
  		ss >> aPerson.age;
  		
  		people.push_back(aPerson);
	}
	cout << endl;
}

void save(ofstream &fout, vector <Person> people)
{
	fout.open("names.txt");
	for(int i=0; i < people.size(); i++)
	{
		fout << people[i].lastName << ';';
  		fout << people[i].firstName << ';';
  		fout << people[i].age;
  		if(i < people.size() -1)
  		fout << endl;
  }
	cout << "Entries saved successfully." << endl;
	cout << endl;
}

void remove(ofstream &fout, vector <Person> &people)
{
	int loc=people.size();
	string remove;
	cout << "Enter the last name of who you want to remove: ";
	cin >> remove;
	for (int i=0; i < people.size(); i++)
	{
		if (people.at(i).lastName == remove)
		{
			loc = i;
		}
	}	
	
	if(loc!=people.size())
	{
		for (int j = loc; j < people.size()-1; j++)
		{
			swap(people[j], people[j+1]);
		}
		people.pop_back();
		cout << "Entry Removed." << endl;
	}
	else cout << "Name not found." << endl;
	
	fout.open("names.txt");
	for(int i=0; i < people.size(); i++)
	{
		fout << people[i].lastName << ';';
  		fout << people[i].firstName << ';';
  		fout << people[i].age;
  		if(i < people.size() -1)
  		fout << endl;
	}
  	cout << endl;
}

void sort(vector <Person> &people)
{
	int n = people.size();
	for (int i=1; i < n; i++)
		for (int j=0; j < n-1; j++)
			{
				if (people[j].lastName > people[j+1].lastName)
				{
					swap (people[j], people[j+1]);
				}
			}
	cout << "Entries sorted successfully." << endl;
	cout << endl;
}


The problem is when the file is opened, the age of the first entry overwrites the age of all other entries. Before calling the open function, it displays, sorts, and saves exactly as it should, but when I reopen the program, the age gets screwed up. It was working before and I haven't changed anything in it as far as I know.

Also, I know I'm not supposed to use EOF, but I can't seem to get it working as intended without, so any suggestions for rewriting it properly without would be appreciated. I know some people have made suggestions, but I was either missing something I did it wrong because I was getting errors.
Last edited on
Move line 89 inside the loop or else add ss.clear(); on line 106.
So that's the purpose of clear! Thank you fg, you've been a great help through this. I have another question if you don't mind. I just tried using ios::app to my save function so that the user does not have to open the file before adding entries to prevent rewriting the file. It works great, the problem is the first new entry gets added the same line in the file as the last entry. It ends up looking like this:

Shatner;William;84
Takei;George;77

If I quit, then try to add another entry, it gets saved to file like this:

Shatner;William;84
Takei;George;77Koenig;Walter;78

I'm guessing the problem is this if(i < people.size() -1) fout << endl; at lines 120 and 121. I understand it starts the next loop on a new line for all entries except the last one so that the end of file isn't a blank line, but that doesn't seem necessary to me. If I'm correct, then getting rid of the if statement and just having fout << endl; would solve this issue, but would that cause any foreseeable problems?
Last edited on
Okay, so similar problem, different program. I'm writing a very similar program to this one, except instead of names, I'm creating a file of journal article entries. Entering and saving data is fine, so is displaying at first. However, once I close and reopen the program and open the file, it erases all the data! No idea why, and as far as I can tell, the syntax is identical except for the variable names.

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
void open(vector <Journal> &article)
/*
 * TASK:     Open file with current entries
 * ACCEPTS:  No input for this function
 * RETURNS:  No pass by value variables
 * MODIFIES: Passes reference of vector
 *			 reconstructing vector of Article
 */
{
	ifstream fin("Catalogue.txt");
	Journal entry;
	string str;
	stringstream strstrm;
	
	while(!fin.eof())
	{
		string tmp;
		stringstream ss;
	
		getline(fin, entry.author,';');
		
		getline(fin, entry.date,';');
		
		getline(fin, entry.title, ';');
		
		getline(fin, entry.journal, ';');
	
  		getline(fin, str);
  		strstrm.str("");
  		strstrm.clear();
  		ss << str;
  		ss >> entry.volume;
  		
  		getline(fin, tmp);
		strstrm.str("");
  		strstrm.clear(); 
  		ss << str;
  		ss >> entry.isbn;
  		
  		article.push_back(entry);
	}
	cout << endl;
}
No idea what's wrong without seeing more of your code. What does the struct Journal look like? What does your save file and save function look like? Also,

34
35
36
37
38
39
40
  		getline(fin, tmp);
		strstrm.str("");
  		strstrm.clear(); 
  		ss << str;
  		ss >> entry.isbn;
  		
  		article.push_back(entry);
Last edited on
Topic archived. No new replies allowed.