Problem generate ID from txt file

I would like to get the staff id information from staff.txt file and generate the new id, but the code that i create it wont work why?

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
void staff::setstaffid()
{
	int count = 0;

	ifstream instaffile("staff.txt", ios::in);

		if(!instaffile)
		{
			ofstream outstaffile("staff.txt", ios::out);
			outstaffile.close();

			ifstream instaffile("staff.txt", ios::in);
		}

		instaffile.read(reinterpret_cast<char *>(this), sizeof(staff));

		if(instaffile.eof())						//if the staff id is 0 then it will start from 20000
		{
			staffid = 1;
			return;
		}
		else
		{
			while(instaffile && !instaffile.eof())
			{
				count++;
				instaffile.read(reinterpret_cast<char *>(this),sizeof(staff));
			}
		}

		staffid = 1 + (count);					//increment the staff id

		instaffile.close();
		return;
}
There's too much faffing about with files when all you want to do is create one if it doesn't exist. There's a flag you can pass to std::ifstream for that. The code has errors, find the flag and removed teh uncecessary code.

Don't test for eof on a stream.
Topic archived. No new replies allowed.