how to find and replace in text file

closed account (L092y60M)
Hello right here I have a program that gives the user the option to create an employee ID, name, address and so on. It also has the option to search employee by employee ID.

What I want to know is how to add and replace employee info. For example employee changes address. Also how to delete employee information from text file.

Thank you very much.

Here's my 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//This program uses file input and output for an employee information system.
#include <iostream>
#include <string>
#include <fstream> 

using namespace std;

int main(){
	ifstream inFile; //declare ifstream as inFile and ofstream as outFile
	ofstream outFile;

	string firstName, lastName, employeeAdd, salary, employeeID, employeeSearch; //declare user inputs as string
	int choice;
	char newEmployeeChoice;

	outFile.open("employee.txt", ios::app); //open employee.txt and append new information

	cout << "Please enter 1: To enter employee information: " << endl; //ask user for what they want to choose for the employee
	cout << "Please enter 2: To look for employee information: " << endl;
	cin >> choice;

	if (choice == 1){ //if choice is 1 user asks employee for information
		do{
			cout << "Please enter your employee ID: "; //user input employee id
			cin >> employeeID;

			cout << "Please enter your first name please: "; //user inputs first name
			cin.clear();
			cin.sync();
			getline(cin, firstName);

			cout << "Please enter your last name: "; //user inputs last name
			cin.clear();
			cin.sync();
			getline(cin, lastName);

			cout << "Please enter your home address: "; //user inputs home address
			cin.clear();
			cin.sync();
			getline(cin, employeeAdd);

			cout << "Please enter in your monthly salary: "; //user inputs monthly salary
			cin >> salary;

			outFile << employeeID << "#" << firstName << "#" << lastName << "#" << employeeAdd << "#" << salary << endl; //output user info in employee.txt

			cout << "\nWould you like to enter in another employee: Y for yes: "; //ask user if they have another entry
			cin >> newEmployeeChoice;
		} while (newEmployeeChoice == 'Y' || newEmployeeChoice == 'y'); //while loop if user inputs yes they can enter in another employee
	}
	else if (choice == 2){ //if choice is 2 the user will have to provide employee ID to bring up information
		outFile.close(); //close employee.txt as outFile
		inFile.open("employee.txt"); //open employee.txt as inFile

		cout << "Please enter in the employee ID of the employee you want to search: "; //ask user for employee ID
		cin >> employeeSearch;

		while (getline(inFile, employeeID, '#'), //while loop so it can recieve all the information from text file
			getline(inFile, firstName, '#'),
			getline(inFile, lastName, '#'),
			getline(inFile, employeeAdd, '#'),
			getline(inFile, salary, '\n'))

		if (employeeSearch == employeeID){ //if statement will compare employee ID to the one in employees.txt and the one user entere, if matches then info is displayed
			cout << "\nEmployee ID: " << employeeID << endl;
			cout << "First Name: " << firstName << endl;
			cout << "Last Name: " << lastName << endl;
			cout << "Employee Address: " << employeeAdd << endl;
			cout << "Employee Salary: $" << salary << endl;
		}

		inFile.close(); //close inFile
	}

	system("pause");
	return 0;

}
What I want to know is how to add and replace employee info. For example employee changes address. Also how to delete employee information from text file.
It's rather simple: Read everything in an array. For delete clear e.g. the employeeID and write back everything but those records with an empty employeeID.

So generally: read all in an array, modify, write all back to file
Topic archived. No new replies allowed.