need some help on my link list

This is a discussion on need some help on my link list within the C Programming forums, part of the General Programming Boards category; hi, im trying to read a string one at a time from a text file separated by space and put ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    19

    need some help on my link list

    hi, im trying to read a string one at a time from a text file separated by space and put it into a link list. i think the part where i read the file and put the string into the link list is wrong. can anyone correct me?

    Code:
    struct fileEntry{
       char *entry;
       struct fileEntry *next;
    };
    
    int main(){
    
      char entryRecord[50];
    
      FILE *myfile;
      myfile = fopen("/home/user/file");
    
      struct fileEntry *headNode, *tailNode;
      headNode = NULL;
      tailNode = NULL;
      
      //allocate memory
      headNode = (struct fileEntry*)malloc(sizeof(struct fileEntry));
      headNode.entry = NULL;
      headNode.next = NULL;
    
      while(fgets(entryRecord, sizeof(entryRecord), myfile))
      {
           headNode.entry = entryRecord;
           headNode.next = NULL;
    
           //set tail node to become head node
           tailNode = headNode;
          
           headNote.entry = NULL;
           headNote.next = NULL;
      }
    }
    Last edited by norhos; 03-14-2008 at 02:01 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    struct fileEntry{
       char *entry;
       fileEntry *next;
    };
    Is wrong, fileEntry doesn't mean anything -- you mean struct fileEntry * next;

    Code:
    void int main(){
    You can only have one type, and the correct type in this case is 'int'. ie
    Code:
    int main(void)
    {
    Code:
    while(fget(entryRecord, sizeof(entryRecord), myfile))
    You mean fgets?

    Also
    * Free any memory you malloc
    * Don't cast malloc
    * Check if fopen() & malloc() didn't fail before continuing
    * Learn how to use a linked list.
    * try compile before you ask why it doesn't work

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    oh sry zac, i actually got the correct code in my vim but i retyped everything here so theres some mistakes.. anyway, how is the inserting of data? is it correct?

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Not really, you have to allocate memory for the next node and so on for n nodes -- allocate as you go, currently you only allocate the first node.

    I assume you want to implement a singly-linked list?

    Also are the records separated by a space or newline in the file? Currently the way you read implies they are separated by a newline character. Try and get a skeleton program working (ie read records and print them to the screen), then add the linked list.
    Last edited by zacs7; 03-14-2008 at 02:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 09:28 PM
  3. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 09:11 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21