extracting string from text file

closed account (L092y60M)
I have a program here that writes employee information into a text file called employee.txt. I don't have a problem writing into the text file.

I'm supposed to enter the employees ID and search for it. Then pull up all they're info. But I am totally lost. Thank you.

This is what I have.
Please note I'm new to C++ and I haven't learned arrays or vectors yet.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){

	ifstream inFile;
	ofstream outFile;

	string firstName, lastName, employeeAdd, salary, employeeID, employeeSearch, line;
	int choice;
	char newEmployeeChoice;
	char inFileName[100], inFileLast[100], inFileAdd[200], inFileSal[100], inFileID[50];

	outFile.open("employee.txt", ios::app);

	cout << "Please enter 1: To enter employee information: " << endl;
	cout << "Please enter 2: To look for employee information: " << endl;
	cin >> choice;

	if (choice == 1){
		do{
			cout << "Please enter your first name please: ";
			cin.clear();
			cin.sync();
			getline(cin, firstName);

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

			cout << "Please enter your employee ID: ";
			cin >> employeeID;

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

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

			outFile << firstName << "," << lastName << "," << employeeID << "," << employeeAdd << "," << salary << endl;

			cout << "Would you like to enter in another employee, Y for yes N for no: ";
			cin >> newEmployeeChoice;
		} while (newEmployeeChoice == 'Y');
	}
	else if (choice == 2){
		outFile.close();
		inFile.open("employee.txt");

		cout << "Please enter in the employee ID of the employee you want to search: ";
		cin >> employeeSearch;
		
		while (!inFile.eof())
		{

			if (employeeSearch == employeeID){
				
				inFile.getline(inFileName, 100, ',');
				inFile.getline(inFileLast, 100, ',');
				inFile.getline(inFileAdd, 200, ',');
				inFile.getline(inFileSal, 100, ',');
				inFile.getline(inFileID, 50, ',');

				cout << inFileName << endl;
				cout << inFileLast << endl;
				cout << inFileAdd << endl;
				cout << inFileSal << endl;
				cout << inFileID << endl;

				cout << firstName << " " << lastName << " " << employeeID << " " << employeeAdd << " " << salary << endl;
			}
		}

		
	}
	system("pause");
	return 0;
}


This is the text file
Viktor,Luc,552,123 Home,5000
Joe,Luc,553,123 House,7000
Look at C++ getline in this page reference.
1
2
3
4
5
6
7
8
9
#include <string>
#include <sstream>
string a;
while(getline(yourfile, a))
   { stringstream b(a);
        while(getline(yourfile, b, ',')) {
           //do the stuff with each info in each , delimited info.
        }
    }
closed account (L092y60M)
@iQChange Is there another way without stringstream?
Don't loop on eof() like this:
1
2
    while (!inFile.eof())
    {


Instead put the file input operation as the while condition. Here, I've put a sequence of operations separated by the , operator. For readability I've used named constants for the comma and newline delimiters.
1
2
3
4
5
6
7
8
9
10
11
12
    const char comma = ',';
    const char newln = '\n';
    
    while (getline(inFile, firstName,   comma),
           getline(inFile, lastName,    comma),
           getline(inFile, employeeID,  comma),
           getline(inFile, employeeAdd, comma),
           getline(inFile, salary,      newln) )
    {
        // here check for matching ID etc.
    
    }
Last edited on
It might not be as good a choice, but you can use scanf, and fgets instead of getline.
closed account (L092y60M)
Thanks for the reply Chervil. But how do I check for matching ID?
But how do I check for matching ID?

use an if statement
closed account (L092y60M)
@Chervil Thanks for your help. I got the program to work. Very much appreciated.
Topic archived. No new replies allowed.