Creating a Linked List from a input file

Posted on 2009-02-23
Last Modified: 2012-05-06
I want to open a file and read the data in to a linked list.

The file is basically
2.0, 2.5
1.2, 3.0
4.5, 5.6

I am not sure how to build the list
Question by:Jaziar
    LVL 24

    Accepted Solution

    Open the file for reading (fopen)
    read in the file line by line (fgets)
    run sscanf on the line to  extract the two doubles in the line
    add the doubles to a list one coul use e.g glib for that.
    #include <glib.h>
    #include <stdio.h>
    static void print_one_double(gpointer data, gpointer user_data) {
        double *d = data;
        if (d)
            printf("%lf ", *d);
    int main(void){
        FILE *in = fopen("t1.txt", "r");
        char buf[128];
        char *pc;
        double d1, d2;
        GSList *list_of_doubles = NULL;
        double *t_double;
        while ((pc = fgets(buf,  128, in)) != NULL) {
            sscanf(buf, "%lf, %lf", &d1, &d2);
            t_double = g_slice_dup(double, &d1);
            list_of_doubles = g_slist_prepend(list_of_doubles, t_double);
            t_double = g_slice_dup(double, &d2);
            list_of_doubles = g_slist_prepend(list_of_doubles, t_double);
        list_of_doubles = g_slist_reverse(list_of_doubles);
        g_slist_foreach(list_of_doubles, print_one_double, NULL);
        /* you have to write a free for the list elements ommitted */
        return 0;

    Open in new window

    LVL 53

    Expert Comment

    If this is an exercise where you have to implement your own linked list, then I suggest reading up on linked lists first. Here's a good starting point :


    Linked lists are explained there using images, pseudo code, and C code.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now