Help deleting person from database

I have tried a lot of stuff to try and make it delete a student from a file...Need some ideas/help please

The struct:

1
2
3
4
5
typedef struct student {
	int stdnum;
	char stdname[];
	int stdgrade;
} Student;


The function:

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
void Table::removestudent() {

	string line;

	fstream stdfile("Student.txt", ios::binary | ios::in | ios::out);

	int removing;

	cout << "Student Number of the student you are removing: " << endl;
	cin >> removing;

	Student *student = new Student;

	while (stdfile.read((char *) student, sizeof(student))) {

		if (removing == student->stdnum) {
			//REMOVE CODE HERE
			break;
		}
	}


	if (removing != student->stdnum) {
		cout << "Student not in the database" << endl;
	}

	stdfile.close();

}
Last edited on
You should open the file in read mode, read all students in an array, except the one you want to delete, close the file and open it in write mode, write the array to the file, then close the file. Done

Vector would be more suitable than an array, arrays you would need to declare the size but vectors can grow in size as per the quantity of students.
Topic archived. No new replies allowed.