[School] Need help fast

I have to write a program for class that has to use persistence and be able to write a file with a made list and then next run be able to recover that list. Any advice on how to get this to work
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

#include <iomanip>
using std::setw;
using std::setfill;
using std::left;

#include <list>

#include <fstream>
using std::fstream;
using std::ios;

void printgrid(class course*, int);
course* push_back(course*, course*);
int myStricmp(const char*, const char*);
int myStrincmp(const char*, const char*, size_t);
int courseCmp(const course*, const course*);
course* insert(course*, course*);
course* deleteHead(course*);
course* restoreList(course*);
void storeList(course*, int);

class course
{
public:
	char cd[33]; // course designation
	char term[7]; // term
	int unit; // units
	char grade; // grade
	course* next; // link
	course* prev; // backwards link
};

int main()
{
	course* head = 0;
	int z = 0;
	int q = 0;
	char restore;
		cout << "\nDo you want to restore class list from previous run? [Y/N]" << endl;
		cin >> restore;
		if (restore == 'Y' || restore == 'y')
			restoreList(head);
	while (true)
	{
		int k = 0;
		for (course* p = head; p; p = p->next)
		{
			printgrid(p, k);
			k++;
		}
		char another;
		cout << "\nDo you want to add a class? [Y/N]" << endl;
		cin >> another;
		if (another == 'y' || another == 'Y')
		{
			course* a = new course;
			cout << "Enter the course description for the new entry. 32 char max [comsc-110]" << endl;
			cin.ignore(1000, 10);			
			cin.getline(a->cd, 33);
			cout << "Enter the term for the new entry. 6 char max [fa2001]" << endl;
			cin.getline(a->term, 7);			
			cout << "Enter the number of units for the new entry [4]" << endl;
			cin >> a->unit;
			cout << "Enter the grade for the new entry [A]" << endl;
			cin >> a->grade;
			head = push_back(a, head);
		}
		if (another == 'n' || another == 'N')
		{
			int y = 0;
			for (course* p = head; p; p = p->next)
			{
				printgrid(p, y);
				y++;
			}
			for (course* p = head; p; p = p->next)
			{
				storeList(p, q);
				q++;
			}
			while (head)
			{
				course* next = head->next;
				delete head;
				head = next;
			}
			break;
		}
	}
	head = deleteHead(head);
	return 0;
}

void printgrid(course* p, int x)
{
	if (x == 0)
	{
		cout << "\nCOURSE      TERM    UNITS  GRADE" << endl;
		cout << "----------  ------  -----  -----" << endl;
	}
	cout << left << setw(12) << setfill(' ') << p->cd << setw(10) << p->term << setw(7) << p->unit << setw(6) << p->grade << endl;
}
course* push_front(course* a, course* h)
{
	a->next = h;
	return a;
}
course* push_back(course* t, course* h) // header 
{ 
  course* p, *prev; 
  for (p=h, prev=0; p; prev=p, p=p->next); 
  t->next = p; // p is zero 
  if (prev) // pointer to last node 
    prev->next = t; 
  else // empty list -- new node is 1st node 
    h = t; 
  return h; 
}
course* deleteHead(course* h) // header 
{ 
  if (h) // zero if already empty list 
  { 
    course* next = h->next; 
    delete h; 
    h = next; 
  } 
  return h; // "h" is new head 
}
int courseCmp(const course* a, const course* b)
{
	//validate the length of the strings
	if ((strlen(a->term) != 6) || (strlen(b->term) != 6))
		return myStricmp(a->cd, b->cd);

	//handle ties here
	if (myStricmp(a->term, b->term) == 0)
		return myStricmp(a->cd, b->cd);

	// compare the years
	int yearA = atoi(a->term + 2);
	int yearB = atoi(b->term + 2);
	if (yearA < yearB)
		return -1; // termA comes first
	if (yearA > yearB)
		return 1; // termB comes first

	// compare semesters in case of the same year
	if (myStrincmp(a->term, "SP", 2) == 0)
		return -1; // termA comes first
	if (myStrincmp(b->term, "SU", 2) == 0)
		return myStrincmp(b->term, "SP", 2) ? -1 : 1;
	return 1;
}
int myStricmp(const char* dst, const char* src)
{
  int f, l;
  do
  {
    if (((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
    if (((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
  } while (f && (f == l));
  return(f - l);
}

int myStrincmp(const char* dst, const char* src, size_t count)
{
  int f, l;
  do
  {
    if (((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
    if (((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
  } while (--count && f && (f == l));
  return (f - l);
}
course* restoreList(course* head) 
  { 
    fstream fin; 
    fin.open("courses.dat", ios::binary|ios::in); 
    if (!fin) 
      return head;

    // read the number of objects from the disk file 
    int nRecs; 
    fin.read((char*)&nRecs, sizeof(int));

    // read the objects from the disk file 
    course* newHead = head; // the new list 
    for (int i = 0; i < nRecs; i++) 
    { 
      course* t = new course; 
      fin.read((char*)t, sizeof(course)); 
      newHead = push_back(t, newHead); 
    } 
    fin.close(); 
    return newHead; 
  }
void storeList(course* p, int x)
{
	fstream fout;
	fout.open("courses.dat", ios::out|ios::binary|ios::app);
	fout.write((char*)&p->cd, sizeof(course));
	fout.close();
}
If you could provide us with info on what's going wrong or not working, maybe we can help.
it starts out with asking if you want to restore from the last run, then it asks if you want to add another class, being a class name, semester, units, and grade. the adding of a class loop goes on until you say no, then it writes it to a file upon exiting. i am having trouble writing it to the file at the end in a way that it can be read again.
basically, the only problem i am having is the writing and then the reading of the file, any help would be greatly appreciated
Topic archived. No new replies allowed.