Input/Output files giving me issues

so i am writing a program that takes in a bunch of information about an employee and will eventually save this information to a txt file. Essentially I add a whole bunch of employees to a vector, if I print out all employees in the vector before exiting the program, all is well. However, if I call the function to save the txt file, then exit the program and reopen it, there is now a 'ghost' employee that is added to the list that doesn't a name or anything. I will just post the functions that open the list of employees and save the list.

function to save file.

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
 void init_ms( MS& ms, int& count, int& sumvar)
{
	_chdir("data");
	ofstream summer("summer.txt");
	ofstream all ("all.txt");
	ofstream idfile ("id.txt");
	ofstream lastyear("lastyear.txt");
	time_t now;
	struct tm *current;
	now = time(0);
	current = localtime(&now);
	int ly = current->tm_year;
	lastyear << ly;
	summer << sumvar;
	int x = count;
	idfile << x;
	for (MS::iterator it = ms.begin(); it != ms.end(); it++)
//	  for(int i= 1; i < ms.size(); i++)
	{
		if (it == ms.begin())
			it++;
		emp c = *it;
	//	  emp c = ms[i];
		all << c.first << " " << c.last << " " << c.ci << " " << c.id << " " << c.ncns << " " << c.sub << " ";
		for (int j = 0; j < 6; j++)
		{
			all << c.day[j] << " ";
		}
		all << c.start << " " << c.summer << " ";
	//	  cout<<c.start<<" "<<c.summer<<" ";
	}
	all.close();
	idfile.close();
	lastyear.close();
	_chdir("..");
}



function to open

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
/**************************
***************************/
void fill_list(MS& ms, int& count, int& sumvar)
{
	_chdir("data");
	ifstream all;
	ifstream idfile;
	ifstream summer;
	idfile.open("id.txt");
	all.open("all.txt");
	summer.open("summer.txt");
	if (all.fail())
	{
		cout << "error";
		exit(1);
	}
	idfile >> count;
	summer >> sumvar;
	
	while (all)
	{
		emp c;
		string str;
		int call, sub;
		int id, ncns;
		all >> str;
		c.first = str;
		all >> str;
		c.last = str;
		all >> call;
		c.ci = call;
		all >> id;
		c.id = id;
		all >> ncns;
		c.ncns = ncns;
		all >> sub;
		c.sub = sub;
		for (int i = 0; i < 6; i++)
		{
			all >> c.day[i];
		}
		all >> c.start;
		all >> c.summer;
	//	  cout<<c.start<<" ";
	//	  cout<<c.summer;
		//ms.insert(c);
		ms.push_back(c);
	}
	summer.close();
	all.close();
	idfile.close();
	_chdir("..");
}



any ideas? let me know if you need to see more of the code.
Topic archived. No new replies allowed.