Thread: reading in file to linked list?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    reading in file to linked list?

    solved
    Last edited by rickyson49; 03-31-2011 at 08:32 AM. Reason: solved

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    warning: 'animal' may be uninitialized in this function.
    warning: 'command' may be uninitialized in this function.
    I don't know how your school is teaching.
    But seriously you're doing linked list!
    And you still don't get pointer!!!!
    Pointer Basics

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    char *animal;                
    char *command;
    
    
     while(fscanf(file, "%d %s %s\n", &time, animal, command) != EOF){
        current = (entry *)malloc(sizeof(entry));
        current->t = time;
        current->ID = animal;
        current->cmd = command;
        current->next = head;
        head = current;
    Your problem is pretty simple really...
    1) You are not allocating memory for your strings.
    2) You cannot assign strings across the equals sign.

    C has no native string type and it doesn't manage memory for you... you have to do that yourself.

    Your struct should look like this...
    Code:
    struct node{
      int t;
      char ID[20];
      char cmd[20];
      struct node *next;
    };
    and you should be creating the struct with malloc BEFORE you read stuff in from disk...
    Code:
    do
      { current = malloc(sizeof(entry));
         current->next = head;
         head = current; }
         while(fscanf(file, "%d %19s %19s\n", current.time,current->animal,current->command) != EOF)
    You will still have problems with it... but this should point you in the right direction.

    Quote Originally Posted by Bayint Naung View Post
    I don't know how your school is teaching.
    But seriously you're doing linked list!
    And you still don't get pointer!!!!
    ... or strings ...

    one does have to wonder. Is it the teacher or the students?
    Last edited by CommonTater; 03-31-2011 at 09:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Text file in Linked list!
    By satty in forum C Programming
    Replies: 20
    Last Post: 07-29-2010, 08:48 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM