File Manipulation Search Record from a File and Edit Files Through Delete and Update Record in a File

Hello, I'm practicing file manipulation again and I'm having difficulty in Search, Delete and Update Record in a file. Can someone help me fix the code below. The updated files after being updatted and/or deleted will also appear in the console. Any help will do 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
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
#include <iostream>
#include <fstream>

using namespace std;
string player;
string role;
string score;

void addrec () 
{
    cout << "Enter player name: ";
    getline (cin,player);
    
    cout << "Enter role: ";
    getline (cin,role);
    
    cout << "Enter score: ";
    getline (cin,score);
    
    fstream myFile;
            myFile.open ("players.txt", ios::out | ios::app);
            if (myFile.is_open ())
            {
                myFile << "Records List\n";
                myFile << "\nPlayer: " << player << "\nRole: "<< role << "\nScore: " << score << endl;
            myFile.close();
            }
}

void displayplayer()
{
   fstream myFile;
   myFile.open ("students.txt", ios::in);
   if (myFile.is_open ())
  {                                                 
     string line;
       while (getline (myFile, line)) 
     {
         cout << line << endl;
    }
}
}

int main()
{
   int pick; 
    
   while (pick != 6) 
    {
    cout << "Player List System\n";
    cout << "1. Add Player\n";
    cout << "2. Search Player\n";
    cout << "3. Display Player List\n";
    cout << "4. Delete Player\n";
    cout << "5. Update Player\n";
    cout << "6. Exit\n";
    
    cout << "Pick an option: ";
    cin >> pick;
    
    switch (pick)
    {
        case 1:
        system ("clear");
        
        cin.ignore();
        addrec();
        break;
        
        case 2:
        //help me search a player in a file and only display the player's records that is searched
        
        //when the searched player is not found, it will display no results found
        break;
        
        
        case 3: 
        displayplayer();
        break;
        
        case 4:
        //help in delete record when you search a player's record you will only delete the player that is searched and
        //that searched player will be only deleted and not the entire file
        
        //when the searched player is not found, it will display no results found
        break;
        
        case 5:
        //help in update record when you search a player's record, you may change its name, role and score
        
        //when the searched player is not found, it will display no results found
        break;
        
    }
    
   
    } 
           
    
   

    return 0;
}

Last edited on
L33 - are you sure???

You can't delete text in the middle of a file. If you want to do this, there are 3 ways.

1) Use a temp file. Copy the data to to temp file you want, skip the data you don't
and then copy the remaining data to the temp file you want. Then delete the original and rename the temp as the original.

2) Read all of the file data into a container such as a vector. Manipulate the data as required. Then write all the data from the container to the file.

3) use a fixed size data structure and random access binary files instead of a text file.

In 1) or 2) the structure of the file should enable easy import of the data. The format used in L25 is not this. In this case a CSV format would be more appropriate as name etc could contain white-space chars.

Unless otherwise indicted - and assuming a text file is needed rather than a binary file - then 2) is usually used.
Based on 2), then consider:

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

struct Player {
	std::string name;
	std::string role;
	unsigned score {};
};

std::istream& operator>>(std::istream& is, Player& play) {
	getline(is, play.name, ',');
	getline(is, play.role, ',');
	is >> play.score;
	return is.ignore();
}

std::ostream& operator<<(std::ostream& os, const Player& play) {
	return os << play.name << "  " << play.role << "  " << play.score << '\n';
}

void addrec(std::vector<Player>& players) {
	Player play;

	std::cout << "Enter player name: ";
	std::getline(std::cin, play.name);

	std::cout << "Enter role: ";
	std::getline(std::cin, play.role);

	std::cout << "Enter score: ";
	std::cin >> play.score;
	std::cin.ignore();

	players.push_back(play);
}

void displayplayers(const std::vector<Player>& players) {
	for (const auto& play : players)
		if (!play.name.empty())
			std::cout << play;
}

void deleteplayer(std::vector<Player>& players) {
	std::string nam;
	size_t cnt {};

	std::cout << "Enter player's name: ";
	std::getline(std::cin, nam);

	for (auto& p : players)
		if (p.name == nam) {
			p.name.clear();
			++cnt;
		}

	if (!cnt)
		std::cout << "Player not found\n";
	else
		std::cout << cnt << " records deleted\n";
}

void search(const std::vector<Player>& players) {
	std::string nam;
	size_t cnt {};

	std::cout << "Enter player's name: ";
	std::getline(std::cin, nam);

	for (const auto& p : players)
		if (p.name == nam) {
			std::cout << p;
			++cnt;
		}

	if (!cnt)
		std::cout << "Player not found\n";
	else
		std::cout << cnt << " records found\n";
}

int main() {
	std::vector<Player> players;
	std::ifstream ifs("players.txt");

	if (!ifs)
		std::cout << "Cannot open file\n";
	else
		for (Player play; ifs >> play; players.push_back(play));

	for (int pick {}; pick != 6; ) {
		std::cout << "\nPlayer List System\n";
		std::cout << "1. Add Player\n";
		std::cout << "2. Search Player\n";
		std::cout << "3. Display Player List\n";
		std::cout << "4. Delete Player\n";
		std::cout << "5. Update Player\n";
		std::cout << "6. Exit\n";

		std::cout << "Pick an option: ";
		std::cin >> pick;
		std::cin.ignore();

		switch (pick) {
			case 1:
				addrec(players);
				break;

			case 2:
				search(players);
				break;

			case 3:
				displayplayers(players);
				break;

			case 4:
				deleteplayer(players);
				break;

			case 5:
				//help in update record when you search a player's record, you may change its name, role and score
				//when the searched player is not found, it will display no results found
				break;

			case 6:
				break;

			default:
				std::cout << "Invalid option\n";
				break;
		}
	}

	std::ofstream ofs("players.txt");

	if (!ofs)
		std::cout << "Cannot open file\n";
	else
		for (const auto& [nam, rol, pnt] : players)
			if (!nam.empty())
				ofs << nam << ',' << rol << ',' << pnt << '\n';
}

Topic archived. No new replies allowed.