Thread: Reading a file into a Linked List

  1. #1
    Unregistered
    Guest

    Reading a file into a Linked List

    I am trying to read a file into a linked list. However, I seem to be having trouble since the printf statement doesn't print anything useful. Here is sample code:

    void main(void)
    {

    struct box *current=NULL;

    FILE *fp;
    fp=fopen("file", "r");

    while(fscanf(fp, "%s%f%f", current->name, &current->x, &current->y) != EOF)

    current = (struct box *)malloc(sizeof(struct box); /*not sure if malloc is useful here*/
    printf("%s%f%f", current->name, current->x, current->y)
    reutrn;
    }

    I am simply trying to scan in the file, but the program will only print the last line in the file, and not all lines. What is wrong here?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >void main(void)
    Use int main(void)

    >fp=fopen("file", "r");
    Always check the file opened correctly before using it.

    >struct box *current=NULL;
    This is a pointer to a struct, not an actual struct. Therefore, you can't do this yet:
    >while(fscanf(fp, "%s%f%f", current->name, ¤t->x, ¤t->y) != EOF)
    There is no memory available for you to store data in until you assign the pointer to a valid area.

    >t->x, ¤t->y
    Where did t come from?

    >/*not sure if malloc is useful here*/
    malloc is useful (required), but not in that particular place.

    >printf("%s%f%f", current->name, current->x, current->y)
    won't work because of the above.

    >reutrn;
    Is spelt wrong. Please cut/paste directly from your source, that way your compiler will highlight the simple mistakes. Also, give it a return code.

    Please use code tags in future.

    Are you trying to load all lines of the file into their own structs (linked list of some sort), or are you just wanting to read one/print one kinda thing?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Allocation of memory for current should be done before you use the variable current. Function malloc does not need casting.

    Besides checking if the file did open correctly, also check the value malloc returns.

    Before the program finishes, the allocated memory must be freed.

    BTW, I don't see anything which has to do with linked lists. I only see that you try to read something in the same struct again and again until end of file is reached.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 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