Thread: Filling a linked list

  1. #1
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127

    Filling a linked list

    Code:
        datafile=fopen("data.abd","r");
        if(datafile!=NULL){
                     firstp=malloc(sizeof(struct person));
                     currentp=firstp;
                     while(1){
                              newp=malloc(sizeof(struct person));
                              fread(currentp,sizeof(currentp),1,datafile);
                              if(currentp->next==NULL){
                                                       break;
                              }
                              currentp->next=newp;
                              currentp=newp;
                     }
                     fclose(datafile);
        }
    This should fill the linked list from a file called "data.txt". For some reason it only fills in the first one. Why?
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    malloc does not null its values when called. All it does is give you a pointer to some place in memory. Just like local non-static variables, they're unintialized upon creation, and contain some random data. That said, you're doing this pretty much ass backwards.

    You should be looping based on the return value of fread, not on if what you've happened to read has somehow magically set the 'next' pointer to NULL or not.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    This is from a file that was already printed from a linked list. I make the linked list in my program and then fwrite it into "data.txt" and read it back next time the program is run. So if currentp->next was NULL when it was printed into the file shouldn't it be NULL when it comes back out?
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    [edit]
    Change your if() to test the return value of fread(), as suggested by Quzah.
    [/edit]
    Last edited by dwks; 04-21-2006 at 03:53 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    But I read the structure from the file into currentp. So doesn't it have that stuff in it?
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    fread(currentp,sizeof(currentp),1,datafile);
    You might want sizeof(*currentp).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Oh, I had that in my program I must have mistyped it into here. How do I make this work though?
    ~Sven
    Last edited by 00Sven; 04-21-2006 at 04:00 PM.
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  8. #8
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    OK I got it. I read through my program a few times and realized that I was writing the entries to the file as soon as they were made. So they were the last entries causing their pointer to be NULL when in the file so it stopped at the first one everytime. Stupid mistake.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Oh, I had that in my program I must have mistyped it into here. How do I make this work though?
    Here's a tip - use copy/paste of your actual code.

    Simply typing here what you think you have isn't good enough. You only need to be off by a very small number of characters before the advice goes off in a completely different direction to your actual problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM