Modify and delete specific file content

how can i delete and modified my file content

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
  #include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
using namespace std;

int main()
{
	string name,program;
	int id,choise;
	ifstream data;
	data.open("data.txt");
	if(data.fail())
	{
		cout<<"\nError Opening The File\n";
		cin.get();
		exit(1);
	}
	
	for(;;)
	{
		cout<<"\nEnter The Student ID : ";
		cin>>choise;
		while(data>>id&&data.ignore(30,'\n'),getline(data,name)&&data>>program)
		{
			if(id==choise)
			{
				cout<<"\nStudent ID : "<<id;
				cout<<"\nStudent Name : "<<name;
				cout<<"\nStudent Program : "<<program;
			}
		}
		data.clear();
		data.seekg(0,ios::beg);
		cout<<"\n1 to try again : ";
		cin>>choise;
		if(choise==1)
		{
			system("cls");
			continue;
		}
		else
		break;
	}
	
	
	return 0;
}


text file data

12345
Muhammad Salman Zafar
BS(CS)
12346
Muhammad Waleed Karam
BS(SE)

if i wanted to delete the 12345 user how to delete it , now it is just displaying the user id , name , program

any tip how to do it








An example of reading from the file into a vector, and deleting from the vector. This program doesn't modify the file. To do that, write out the contents of the vector to the file after making changes.
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

struct Student {
    int id;
    string name;
    string program;
    
};

// Function to read a Student  from a file
bool readStudent(std::istream & is, Student & st)
{
    is >> st.id;   is.ignore(100, '\n');
    getline(is, st.name);
    getline(is, st.program);
    return is;
}

// Function to display a Student
void showStudent(std::ostream & os, const Student & stud)
{
    os << "\nStudent ID :      " << stud.id;
    os << "\nStudent Name :    " << stud.name;
    os << "\nStudent Program : " << stud.program;
    os << '\n';
}

int main()
{
    ifstream data("data.txt");
    
    if (!data)
    {
        cout << "\nError Opening The File\n";
        cin.get();
        return 1;
    }
    
    // Define a vector to store the entire file contents
    std::vector<Student> students;
    
    
    // Read all Students from file into vector
    Student stud;
    
    while (readStudent(data, stud))
    {
        students.push_back(stud);
    }
    
    
    char choise = '1';
    
    while (choise == '1')
    {
        // Display all the contents of the vector
        for (size_t i=0; i<students.size(); ++i)
            showStudent(cout, students[i]);

        cout << "\nEnter The Student ID to be deleted : ";
        int id;
        cin >> id;

        // find and delete Student from vector
        for (auto it = students.begin(); it!=students.end(); ++it)
        {
             if (id == it->id)
             {
                 students.erase(it);
                 break;
             }            
        }

        cout << "\n1 to try again : ";
        cin >> choise;
        
    }
}

Student ID :      12345
Student Name :    Muhammad Salman Zafar
Student Program : BS(CS)

Student ID :      12346
Student Name :    Muhammad Waleed Karam
Student Program : BS(SE)

Enter The Student ID to be deleted : 12346

1 to try again : 1

Student ID :      12345
Student Name :    Muhammad Salman Zafar
Student Program : BS(CS)

Enter The Student ID to be deleted : 0

1 to try again : 0
Topic archived. No new replies allowed.