?
Solved

Creating a Linked List from a input file

Posted on 2009-02-23
2
Medium Priority
?
534 Views
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

etc
I am not sure how to build the list
0
Comment
Question by:Jaziar
2 Comments
 
LVL 24

Accepted Solution

by:
fridom earned 500 total points
ID: 23719396
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);
    putchar('\n');
    /* you have to write a free for the list elements ommitted */
 
 
    return 0;
 
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23719705
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 :

        http://en.wikipedia.org/wiki/Linked_list

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Suggested Courses

621 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