Reading txt file and processing txt

I'm trying to write a program that opens a txt file if it exists and reads user information from there within. Now this requires me to read individual lines then split them based on certain delimiters.

I may not have come up with the best solution but here's what I got:

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 FileReaderAndy::readStaff(vector<StaffAndy>& staffIn)
{
    ifstream file;
    string line;
    file.open("staff.txt");
    file.close();
    //checks to see if the initiate file exists
    if(file.fail())
    {
        staffIn.push_back(StaffAndy("admin", "0000", true));
    }
    else
    {
        file.open("staff.txt");
        while(!file.eof())
        {
            int delim = 0;
            getline(file, line);
            vector<string> staffStrings;
            do
            {
                delim = line.find("-");
                string split = line.substr(0, delim);
                staffStrings.push_back(split);
                line.erase(0, delim);
            }
            while(line.size() != 0);

            do
            {
                string account = staffStrings.back();
                delim = account.find("+");
                string name = account.substr(0, delim);
                account.erase(0, delim);
                delim = account.find("+");
                string isAdmin = account.substr(0, delim);
                account.erase(0, delim);
                delim = account.find("+");
                string password = account.substr(0, delim);
                if(isAdmin.compare(ADMIN) == 0)
                {
                    staffIn.push_back(StaffAndy(name, password, true));
                }
                else
                {
                    staffIn.push_back(StaffAndy(name, password, false));
                }
                staffStrings.pop_back();
            }
            while(staffStrings.size() != 0);
            }
    }
}


Now that guy will compile, but i feel there has to be a better way to do that. Any Ideas?
If all the data for each staff member consists of the same thinge
E.g.
Name
Age
Occupation

You can actually save all that data in its object form and read the entire object. Meaning you do no have to do anything fancy to load/save except a single while loop. So you can load all the staff members objects into memory, process them, and when your done, save.

Sometimes people can make things more complicated than they need to be, largely due to being unaware such facilities or methods exist. Unfamiliar with your environment.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const char* FILENAME= "staff.txt";
vector<StaffAndy>* staff;

//Load all
ifstream in(FILENAME);

while(!file.eof())  //Not end of file
{
   StaffAndy* temp;
   in.read(temp, sizeof(StaffAndy));  //Must be C string due to known size of each entry ;) "char* name"
   staff.push_back(temp);
}

//Save all
ofstream out(FILENAME, ios::trunc);   //Clear files contents for writing

for(int i = 0; i < staff.size(); i++)  //Loop through all the staff
{
   out.write(staff[i], sizeof(StaffAndy));
}


This will load and save all the data, no need for doing every single data member or using delimiters and all that jaz.
Last edited on
Topic archived. No new replies allowed.