Need help with little things...

So I have gotten the basis of this down... I just need to add a few more things but I am unsure about how to do this. I don't know how I would go about deleting a file? Also... whenever I try to display the ID, it doesn't appear/work. So If I typed in information... Then went to display is (Since its the first one I'd choose Student ID # 1) and it would come back with invalid data? Any Idea why? 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
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


struct Student  {
	
	int   id;			// Student ID
	char  name[30];		// Student Name
	int   points;	    // Points earned 
	float gpa;		    // GPA
};

int getOption () {
	int selection;
	cout << "Options :\n";
	cout << "1) Initialize the student file\n";
	cout << "2) Add a new student\n";
	cout << "3) Display a student\n";
	cout << "4) Update a student\n";
	cout << "5) End\n\b";
	cout << "Please select an option : ";
	cin >> selection;
	while (selection < 1 || selection > 5) 	{
		cout << "Please select a number between 1-5\n";
		cin >> selection;
	}
	return selection;
}

void UpdateStudent(fstream &fileStudents)  {
	Student student;
	long recNum;
	// Get the Student ID to change
	cout << "Enter the student ID that you want to change:";
	cin >> recNum;
	// Move to the record and read it.
	fileStudents.seekg(recNum * sizeof(student), ios::beg);
	fileStudents.read(reinterpret_cast<char *>(&student),
		           sizeof(student));
	// Display the record contents
	cout << "Name : ";
	cout << student.name << endl;
	cout << "Points Earned : ";
	cout << student.points << endl;
	cout << "GPA : ";
	cout << student.gpa << endl;
	// Get the new record data.
	cout << "Begin entering the new student data :\n";
	cout << "Name : ";
	cin.ignore();
	cin.getline(student.name, 30);
	cout << "Points Earned : ";
	cin >> student.points;
	cout << "GPA : ";
	cin >> student.gpa;
	
	// Set position to start of the student record
	fileStudents.seekp(recNum * sizeof(student), ios::beg);
	fileStudents.write(reinterpret_cast<char *>(&student),
		            sizeof(student));
	
}

void DisplayStudent(fstream &fileStudents) {
	Student student;
	long id;
	// Get the student ID to display
	cout << "Enter the student ID to view ";
	cin >> id;
	// Move to the record and read it.
	fileStudents.seekg(id * sizeof(student), ios::beg);
	fileStudents.read(reinterpret_cast<char *>(&student), sizeof(student));
	// Show the student's data
	cout << "ID           : ";
	cout << student.id << endl;
	cout << "Name         : ";
	cout << student.name << endl;
	cout << "Points earned: ";
	cout << student.points << endl;
	cout << "GPA          : ";
	cout << student.gpa << endl;
	
}

void AddStudent(fstream &fileStudents) {
	cout << "Please enter the Student information : \n";
	Student student;
	
	cout << "Name : ";
	cin.ignore();
	cin.getline(student.name, 30);
	cout << "Points earned : ";
	cin >> student.points;
	cout << "GPA : ";
	cin >> student.gpa;
	
	
	fileStudents.write(reinterpret_cast<char *>(&student),
		            sizeof(student));
	
}

void InitializeStudentFile(fstream &fileStudents) {
	Student student = {0, " ", 0, 0.0};
	for (int id = 0; id < 100; id++) {
		fileStudents.write(reinterpret_cast<char *>(&student), sizeof(student));
	}
	
}


int main(int argc, char * argv[]) {	
	int option;
	// Create a random access file and initialize with 100 empty records
	fstream fileStudents ("Students.dat", ios::out | ios::binary);
	

	do	{
		
		option = getOption();
		switch (option) {
	      case 1: InitializeStudentFile(fileStudents);
			break;
		  case 2: AddStudent(fileStudents);
			break;
		  case 3: DisplayStudent(fileStudents);
			break;
		  case 4: UpdateStudent(fileStudents);
			break;
		  case 5: cout << "bye\n";
		}
	}  while (option != 5);

	fileStudents.close();
	return 0;
}
> I don't know how I would go about deleting a file?

Perhaps you just want overwrite a file, discarding its previous contents.
1
2
3
std::ofstream file( "file_name" ) ; 
// this creates a new file if the file does not exist
// otherwise it truncates the existing file to an empty file.  


Ideally, when you want to perform only input, as in DisplayStudent(), use a std::ifstream. For only output, as in AddStudent(), use a std::ofstream.



> whenever I try to display the ID, it doesn't appear/work.
> ... and it would come back with invalid data? Any Idea why?

See: http://cplusplus.com/forum/general/69685/#msg372532


Overwriting a file won't do anything good for me.

I need to be able to remove a record from the list of records I already have.
This means that I could add record 101, then I need to delete it by pressing 6 and then selecting that Student ID 101. Then it no longer exists.

I fixed it so that it would actually tell you the record number when adding a person. Now All I need to do is be able to remove that person from the record.
Last edited on
> Overwriting a file won't do anything good for me.
> I need to be able to remove a record from the list of records I already have.

a. Read all the records in the file into memory.

b. Overwrite the file record by record, skipping the record that you want to remove.
I think
JLBorges
has pointed a good option because if at any time you have a problem/crash in the program may result in a damaged file. So a better way could be
open file
Read all data and sort it
suggestion :-
Student studentContainer[MAX_LIMIT];
read each and place studentContainer[recNum] in the array
close the file

Work on your data

now if a Student at recNum is removed, then you may replace it with a empty
1
2
3
4
5
6
7
8
9
10
11
Student empty;
empty.id = 0;
void RemoveStudent(Student studentContainer[]; int recNum){
  studentContainer[recNum] = empty;
}
bool StudentEmpty(Student studentContainer[]; int recNum){
  if(studentContainer[recNum] == empty)
    return 1;
  else
    return 0;
}

now use it in Display, Add, Update Student
1
2
3
4
if (StudentEmpty(studentContainer[],recNum))
  //what you want to do if the record is empty
else
  //what if record is there 


when finished open file
overwrite the file with all new data
close the file
Last edited on
Topic archived. No new replies allowed.