PDA

Click to See Complete Forum and Search --> : Linked lists and read from file


onerrorgoto
Feb 14th, 2001, 02:43 AM
Hello
We are studying C at college in sweden, just started last weekand are stuck at this with linked lists.
(Working in VC++)
Using a struct looking like this(public declared)


struct stud
{
char kursid[10];
char pnr[12];
char name[30]
int test_score;
struct stud *spek;
};

struct stud *rot,*ny;
FILE *fp;

first we want to add item to the end of the list thru a function declared like this:
int laggtill(struct stud **studpek);

We have managed to add items to the front of the list but we can not add the new items to the end of the list.
"visually" we have achieved this:
Root->item3->item2->item1
but we want it to look like this:
root->item1->item2->item3


That was the first part of our problem.
We have succeded to save the struct to a file binary thru this code:

void savetofile(struct stud *studskriv)

while(studskriv!=NULL)
{
fwrite(studscriv,sizeof(struct stud),1,fp);
studskriv=studskriv->spek;
}


How do we read the file? we have tried to use fread thru this code:

void readfromfile(struct stud **studpek)
{
int numread;
struct stud *new;

//open file
//check for error in open

//allocate memory for the struct


while((foef(fp))==0)
{
new=(struc stud *)malloc(sizeof(struct stud));
//check for malloc error

new->spek=*studpek;
*studpek=new;

numread=fread(*studpek,sizeof(struct stud),1,fp);
}

}//end function


This adds item to the "front" of the list, as in the first Q, How do we add to the end of the list?

Any help apriciated, we have been stuck with this for 4 days now and are getting desperate.

Thank you
Anders & Kent

HarryW
Feb 14th, 2001, 06:46 AM
It's hard to figure it out in my head because by the looks of it, those variables are all named in Swedish :rolleyes:

One thing that I'm surprised if it's not giving you an error in MS VC++: you use a variable named new. In C++ (not C, but you're using a C++ compiler) new is a keyword, since it is an operator for allocating memory, like malloc(). I wouldn't use it if I were you, I'd use some other, similar name, like new_node or something.

I would try to figure out what's going on but I don't have much time (I have to leave in a minute) and it's not obvious to me where the problem is. Sorry.

onerrorgoto
Feb 14th, 2001, 07:06 AM
Thanks for the reply.

Well the most of the var-names are in swedish but the functionnames are "translated" so that hopefully it would be easier to understand what the functions do.

struct stud *new is translated from our app. In my app the real name is *ny. (which is the same as new i english) Sorry for the bad choice of names. I was trying to make the code more readable.

The struct is for a list of student names and their grade's and SSN.

The app will (hopefully) be able to alow the user to add, remove, search, save and read to file.

parksie
Feb 14th, 2001, 08:34 AM
For linked lists you can use the STL list template class, which is fast and a doddle to use -- SGI has documentation on their website.