File I/O: writing linked list

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:

Can anyone help?
Last edited on
I think your code has two problems. The first one that the loop in my opinion is invalid

for (p=head;p;p = p->next, nRecs++)

What is the sense of using nRecs? It is not used in the loop.

The second problem is that you are trying to save a pointer instead of data membbers of your list.
Hmmm.....ok...

What I think is:
I want nRecs++)counting how many nodes in the list
then wring such number of nodes into file.

I'm not sure whether i did it right or not. Could you give some hints about how to fix it?
Last edited on
You should show the definition of the course.
Last edited on
1
2
3
4
5
6
7
8
9
class course
{
public:
	char name[12];
	char term[8];
	int unit;
	char grade;
	course*next;
};
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 )
Last edited on
Ya...i think that's my problem.

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.
Last edited on
OK, i chaged it to this which looks making more sense. But it's still not working correctly.




Can someone tell me where is wrong?
Last edited on
The wrong thing is my fin function can't read the file that from fout, it will just stuck at there.
Last edited on
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.





Last edited on
Thank you very much for helping me out.

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.
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.
Last edited on
Thank you for helping! Is that possible to write it to 1 function?


I can't make it 2 functions because assignment only allow me to write it into 1 function.

I was thinking about this one after reading ur post. But it's still not working. :(


I think it's because the for loop is not only updating nRecs but also add one more fout every time.
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.