Results 1 to 4 of 4

Thread: Linked lists and read from file

  1. #1

    Thread Starter
    Hyperactive Member onerrorgoto's Avatar
    Join Date
    Aug 1999
    Location
    Sweden
    Posts
    330

    Question

    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)

    Code:
    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:
    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:
    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
    Onerrorgoto

    Dont be to optimistic, the light at the end of the tunnel might be a train

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    It's hard to figure it out in my head because by the looks of it, those variables are all named in Swedish

    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.
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Hyperactive Member onerrorgoto's Avatar
    Join Date
    Aug 1999
    Location
    Sweden
    Posts
    330

    Red face Sorry for bad var-names

    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.
    Onerrorgoto

    Dont be to optimistic, the light at the end of the tunnel might be a train

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width