Here is the problem: I made a class course, then I need to write them into file. Next time when I run program, i will read it first then write it again.
But my fout function is not working correctly. Here is the code:
And what are you going to save? In your previous example you was trying to save only four bytes (if the int has size of four bytes on your system) that is the value of sizeof( int )
User will put their course name, term, unit and grade, them program should save all these data into file by using these codes.
Read File if there is any.
User put new course if they want.
Then program write them into file.
Z, I've just taken a look at UR code and the comments by yourself & others.
It looks to me that your are about to get wrapped-up in the mechanics of the code, and become fairly lost fairly quickly.
Stop for a moment, Take a breath, then take a pencil and some paper especially a sheet of graph paper.
On the paper, draw-out a single class and show how it is moved into a new class node (from the first node or previous node - depending on whether it's existing data or not )
Then- on a second sheet of paper - write out the general steps involved.
Pseudo code is best for this step as you won't have to worry about syntax.
should read something like this ...
1- define an empty class node
2- enter class data from screen(user) or file
3- increment node cntr
4- move data into node
5- ask user for more or stop
6- continue ( or END)
VLAD is right that nRECS is useless as it now stands - It counts, but does nothing.
Your IF (!FIN) ... would read better with an ELSE rather than two seperate rtn's. ( but that's minor)
I would have to see some results for SIZEOF(COURSE) but it looks like that may be pointing you down the wrong avenue. You seem to want a tally of the number of nodes. Keeping track of the sizeof class will be hell to debug if the class structure ever needs to be changed. ( for example, if you decide to add a first name or last name or student's age or a classroom number, then the "SIZEOF" will change accordingly and you'll have to re-compute the formula to get the right number of nodes again - Better just to simply maintain a node counter in the first place - with a good understandable name ;
since it will be an INT , then something like: i_Node_Cntr.
Once the steps are written on paper ( or notepad), then you can use the steps which you've layed out to create your subrt'ns, and therefore your code will follow.
IF you then post you pseudo code along with your code (again), then I'll be happy to review it.
I'm just saying that presently it's difficult to read, and more difficult to understand exactly what your requirements are.
This assignment actually is as same as my last one except the adding persistence part, which is also the place I am stuck. So i think the logic part is right.
I really can't print all my codes here because the assignment hasn't been due yet. I'm really sorry about that.
This the general codes:
So the main problem is my saveList function is not working correctly, the nested for-loop are repeating getting my courses. it's like: 1st course; 1st, 2nd courses; 1st, 2nd, 3rd courses... <------that's how it works. But I still don't know how to improve it.
and also, my reading file function can't read anything from the output file, which is driving me crazy.
my reading file function can't read anything from the output file, which is driving me crazy.
Of course it can't. Your restoreList expects the first item in the file to be an int specifying the number of records in the file. Your saveList doesn't make sure the first item in the file is an int specifying the number of records in the file.
Reading and writing the next pointer to and from the file is not a good thing to do, but you can get away with it as long as you remember when you're reading the file that it doesn't contain a valid value.
int getSize(const course* head)
{
int nRecs = 0 ;
while ( head )
{
++nRecs ;
head = head->next ;
}
return nRecs ;
}
void saveList(course* head)
{
fstream fout( "courses.dat", ios::binary | ios::out ) ;
int nRecs = getSize(head) ;
fout.write(static_cast<char*>(&nRecs), sizeof(nRecs)) ;
while ( head )
{
fout.write(static_cast<char*>(head), sizeof(*head)) ;
head = head->next ;
}
}
Depending on your push_back function, you may need to set the value of c->next to 0/NULL/nullptr before feeding it to push_back in restoreList. The value you're reading from the file for that is not going to be valid. You may also want to consider if the list you read from the file should end up in the same order as the list you wrote to the file.
You only want to write nRecs to the file one time and that time must be before everything else you write to the file. Writing it nRec times to the file between records isn't going to do much for you other than screw up your file format.
There are two approaches you can take here: Figure out what nRecs should be before you write it, then write it. The second approach is to just write a dummy variable at the beginning. Then after the loop, seek back to the beginning of the file and write the actual value over the dummy value in the file.