reading and editing a txt file in console window

Hello everybody, this is my first post in a forum like these so sorry if i'm not completely clear about some things. I'm trying to build a system for a kids playcentre. Everything seemed to have been runing pretty smoothly apart from this.

What I am trying to do, is to read the contents of a text file so that the user can then edit the file within the console program. I can read the contents of the text file, However I am unable to edit it correctly. This is all for an assignment which is due in May.

E.g. 1. The user should be able to select option 5 to add an employee.

2. The user should then be able to select option 7 to view the records within the file.

3. The user should then be able to select option 6 to edit a specific record.

My thoughts initially are to open the file and then use STD::string to do the actual editing. Taking a string from the user and then comparing it to text in the file and replacing if necessary. However, I am completely stumped as to how to begin this.

Many thanks to everyone in advance, please let me know if I can be of any more assistance.

Any help with this would be much apprieciated.


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
 

#include <iostream>
#include<fstream>
#include<sstream>
#include<conio.h>
#include<stdio.h>
#include<iomanip>
#include<string>

using namespace std;
/////////////////////////////
//////CLASS EMPLOYEE///////////////
class Employee
{
      private:
      int EmployeeID, EmployeeFirstName, EmployeeLastName;
      double Wage;
      
      public:
      void AddEmployee()
      {

     string EmployeeID, EmployeeFirstName, EmployeeLastName;
     double Wage=0.00;
     ofstream fout;
     fout.open("EmployeeRecord.txt",ios::app);    // open file for appending


     cout<<"Enter Employee ID: ";
      cin >> EmployeeID;

      cout << "\n";
     cout<<"Enter Employee First Name: ";
     cin >> EmployeeFirstName;
     cout << "\n";
     cout<<"Enter Employee Last Name: ";
     cin >> EmployeeLastName;
     cout << "\n";
     cout<<"Enter Employee Wage: ";
     cin >> Wage;
     
     
     fout << setiosflags(ios::fixed | ios :: showpoint)
        << setprecision(2);
            fout<<  EmployeeID<< ", " << EmployeeFirstName
                << " " << EmployeeLastName<< " , " << Wage << endl;
            fout << fixed;

     fout.close( );       //close file
   ;

}
                       
};
//////////////////////////////////////
//////VIEW EMPLOYEE ON RECORD/////////
void ViewEmployeeRecord()
{
    string line;
    ifstream x ("EmployeeRecord.txt");

    if (x.is_open())
    {
    while(!x.eof())
    {

    cout<<endl;
    getline(x,line);
    cout<<line<<endl;

    }
    x.close();
    }
    else
    cout<<"Cant open file."<<endl;
}

    


/////////////////////////////
//////FUNCTION MENUSCREEN///////////////    
void MenuScreen()
{
     loop:
     cout << "\n\nPlease select what you would like to do\n\n";
     cout << "[1] Add a new customer\n\n";
     cout << "[2] Edit an existing customer\n\n";
     cout << "[3] Make a party booking\n\n";
     cout << "[4] edit a party booking\n\n";
     cout << "[5] Add a new employee\n\n";
     cout << "[6] edit a current employee\n\n";
     cout << "[7] View employee's on record\n\n";
     
     int selection;
     cin>>selection;

    cout<<""<<endl;
     if (selection==1)
        {
            system("cls");
            cout << "You have selected option 1\n\n";
            system("cls");
            goto loop;
        }
       else if (selection==2)
        {
            system("cls");
            cout << "You have selected option 2\n\n";
            system("cls");
            goto loop;
        }
        else if(selection==3)
        {
            system("cls");
            cout << "You have selected option 3\n\n";
            system("cls");
            goto loop;
        }
        else if(selection==4)
        {
            system("cls");
            cout << "You have selected option 4\n\n";
            system("cls");
            goto loop;
        }
        else if(selection==5)
        {
            system("cls");
            cout << "You have selected option 5 to add a new employee\n\n";
            Employee NewEmployee;
            NewEmployee.AddEmployee();
            goto loop;
        }
        
        else if(selection==6)
        {
            system("cls");
            cout << "You have selected option 6 to edit an employee\n\n";
            system("cls");
            goto loop;
        }
        else if(selection==7)
        {
            system("cls");
            cout << "You have selected option 7 to view employees on record\n\n";
            ViewEmployeeRecord();
            goto loop;
        }
        
                else
        {
            cout<<"Invalid! Try again."<<endl;
            goto loop;
        }

}




/////////////////////////////
///////MAIN///////////////
int main () 
{

MenuScreen(); 
  
 cout << "Press any key to continue\n";
 char response;
 cin >> response;
 return 0;
}


Well here are a few ideas:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    You are going to need to search for the employee record to edit.
    If you find the record:
        For each field in the record,
            Take the data that the user types in and store it in a temporary field.
        Verify from the user that the user wants to overwrite the data.
        If true:
            For each temporary field,
                Replace the record data with the temporary data.
            Write the new record to your file.
        else:
            Close the file.

    If you don't find the record:
        Display a message stating so.
 
thanks a lot Kooth, that has been very helpful. Well I've got the search bit working and am going to look at the rest of it tonight.
Once again many thanks.

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
 void EmployeeSearch()
{
    ifstream data("GROUP.txt");
    string item,line;
    int x=0;
    int y=0;
    string id;

    cout<<endl;
    cout<<"Enter Employee Id: ";
    cin>>id;
    cout<<""<<endl;

    while(!data.eof())
    {
        getline(data,line);
        string item_array[10];
        stringstream stream(line);
        x=0,y=0;

        while(getline(stream,item,','))
        {
            item_array[x]=item;
            x++;
            item_array[y]=item;
            y++;
        }

        if(item_array[0]==id)
        {
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tID:   " <<right<<"  "<<item_array[0]<<endl;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tFirst Name: " <<right<<" "<<item_array[1]<<endl;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tLast Name: " <<right<<" "<<item_array[2]<<endl;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tWage: " <<right<<" "<<item_array[3]<<endl;
            cout<<""<<endl;
        }
    }
    data.close();
} 


i'm thinking about taking in user input for First Name, Last Name and Wage either as seperate values or as an array and then something like
1
2
3
4
  

NewFirstName == item_array[1];
NewLastName == item_array[2];  

[/code]
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

//////////////////////////////////////////
//////CLASS FUNCTION EmployeeSearch//////////
void EmployeeEdit()
{
    ifstream data("GROUP.txt");
    string item,line;
    int x=0;
    int y=0;
    string id;

    cout<<endl;
    cout<<"Enter Employee Id: ";
    cin>>id;
    cout<<""<<endl;

    while(!data.eof())
    {
        double wage; 
        
        char Choice;
        getline(data,line);
        string item_array[10], NewWage;
        stringstream stream(line);
        x=0,y=0;

        while(getline(stream,item,','))
        {
            item_array[x]=item;
            x++;
            item_array[y]=item;
            y++;
        }

        if(item_array[0]==id)
        {
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tID:   " <<right<<"  "<<item_array[0]<<endl;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tFirst Name: " <<right<<" "<<item_array[1]<<endl;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tLast Name: " <<right<<" "<<item_array[2]<<endl;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tWage: " <<right<<" "<<item_array[3]<<endl;
            
            cout << "Would you like to change the Wage? Y/N ";
            cin >> Choice;    
            
            if (Choice == 'y')
            {
            cout << "What would you like to change the wage too?";
            cin >> NewWage;
            
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tCurrent Wage: " <<right<<" "<<item_array[3]<<endl;
            
            item_array[3] = NewWage;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\New Wage Wage: " <<right<<" "<<item_array[3]<<endl;
            }
        }
    }
    data.close();
}

 


I've managed to search and retrieve the the values from the file and can now edit them easily and return them to the file as a new record. Thank you so much for your help.

The only thing I'm confused about now is how i can delete the old record so that the file looks like its been edited.

Thanks to anyone else who contributes as well
There are several ways to do this. One way that comes to mind is to somehow mark the record as deleted (duh!) and then at some point rewrite the entire file omitting the records marked as deleted.
I did a whole lot of this stuff a while ago and what u've kind of done aswell, but do you want to delete and overwrite the WHOLE file or just a single record
Why not just keep it simple and read the whole file into memory, make your changes and write the whole thing back to disk? Text files aren't large or complicated so there's no need to complicate things. Also I would suggest trying something like CSV or TAB delimited text in order to help organize your data better.
The problem i'm having is how to delete the record after it has been loaded in to memory.
Once it has been loaded in to something like the array I used above would it be a case of assigning item_array[1-4] to a space or null value or something similar?

I can use this code to read the contents of the entire 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

//////////////////////////////////////////
//////CLASS FUNCTION DisplayAllEmployees//////////
               
void DisplayAllEmployees()
{
    string line;
    ifstream x ("GROUP.txt");

    if (x.is_open())
    {
    while(!x.eof())
    {

    cout<<endl;
    getline(x,line);
    cout<<line<<endl;

    }
    x.close();
    }
    else
    cout<<"Cant open file."<<endl;
}
};


Does anyone have any example code I can look at or anywhere with clear explanations. Everyone who's commented so far has been really helpful, will get there soon enough hopefully!!
@jammas615:

either way really buddy, as long as it results in the file being changed. I'm not too bothered about how it's done. Whichever is the simplest and most efficient which by the sounds of things seems to be to delete and rewrite file.
Once you read the data from the file into the class objects you don't touch the text file again until the end when you write the data back. So forget about reading the file in at every function. Also, no, you cannot use that function to read the data in because it doesn't use your class, I don't know who your teacher is but I know you'll lose points for it.

You want to load your "Employees" into a vector, if you absolutly have to use an array I'll forgive you but please TRY to use a vector. From here it's just a matter of searching against the specific data member like "EmployeeName" or "EmployeeID" to find the record you wish to address. Then you make your changes to that record. DO NOT TOUCH THE TEXT FILE.
Last edited on
I'm not trying to be mean here, but your Employee class is useless so far. You should not be using functions that aren't member functions of that class for EmployeeEdit(), it could be that this code is supposed to be inside of your class delcaration but I have no way of knowing that.
To display your employees from a global vector you should use something like this:
1
2
for(int i = 0; i < EmployeeVector.size(); ++i)
{std::cout << EmployeeVector[i].How_You_Read_Data_From_Your_Class() << '\n';}


See here: http://www.cplusplus.com/reference/stl/vector/
Sorry, yes, EmployeeEdit() is in the declaration of the class.

I've been reading in to Vectors and i think i understand what you mean.

1. so my Employee class would contain a function which creates an array of Vectors to which the employees are read in from file. (similar to EmployeeSearch() but using a vector)

2. File is then closed.

3.Using the vector elements, assign the appropriate elements with user inputted data. I.e user changes the element within the vector.

4. After required changes to the record have been made, the file is reopened and cleared and the new vector is written to the file.

Does this make sense??
Topic archived. No new replies allowed.