Alright, so I'm working on a project where I need to make an HR program to read a manager and it's employees. You know the whole login, edit info, display info, add employee, etc. etc. And I've completed most of it, but I was curious as to how I would go about editing specific things from the text file where all the info is stored. The way I have it now all of the information for each specific employee is on one line (easier to find and display information). Would I just have the user input what the current thing is that they want to change, search for it, than change it? Would I have it search for the user who is asking and display each of their items they can change and have a switch function to ask what they want to change? (<-- *this is the one I would like to do if possible). Or is there some vast easier way that is just escaping my mind?
Details:
For anyone that is willing to help some of the things you might want to know are that, 1) the manager can change any information from anybody, 2) the employee can only change a set amount of things from his/her own information (I'm thinking of making 2 separate functions to do 1 and 2), 3) I'm still kind of new to C++ so if you could please make any information easy to understand, and 4) I have not started on this bit of the code yet, but if you think you need me to display something from the rest of my code just let me know.
Unless working with a shared database such as SQL, (which isn't applicable here) the easiest way to do things is to Open the file once when starting the program, and saving the file only upon exit or explicit request from the user. The saving will replace the entire contents of the file.
How I'd suggest is to:
a) Define a structure with all of the fields that you need for a single entry.
b) Make an array of these structures.
c) Open the file and copy the data to the correct fields in the structure.
d) Close the file
You now have all of the data in memory and in a usable format. Loging, edit, display, add as you like. When done, you will want to save the file.
When you want to save, just create a new ofstream object, and over-write what is currently in the file. Do the exact opposite of the read function that you had made before. Close the file.
That was pretty much what I did to read the file to add/delete/view the employee, except instead of the array I used getline() to find and output/delete that line.
So, by using the array would that read the entire line, or would it read the individual strings that make up the line? And how would I then find the specific part I want to change? Would I input what I want to change then use while(!datafile.eof()) to search through until I find the name I want to change?