1

File Format:

7   49
73  58
130 72
144 78
123 9
40  65
92  42
187 3
127 29

I have to read these numbers (has 50 such lines) in the way Xvalue .... Yvalue in the linked list. I am having trouble reading the numbers. I have created the linked list as follows:

    list L;
    node *p, *q;
    int i;

    L = (list *)malloc(sizeof(node));
    L -> x = L -> y = 0;
    L -> nextx = L -> nexty = NULL;

    FILE *fp;
    fp = fopen("points.txt", "r");
    if (fp == NULL) 
    {
        fprintf(stderr, "Error: unable to open file...\n");
    }

    p = L;
    for (i=0; i<100; ++i) 
    {
        q = (node *)malloc(sizeof(node));
        q -> x =  // store x values here
        q -> y =  // store y values here
        q -> nextx = q -> nexty = NULL;
        p -> nextx = p -> nexty = q;
        p = q;
    }
7
  • 1
    Simply fscanf(fp, "%d%d", &(q -> x), &(q -> y));? Commented May 4, 2020 at 4:43
  • 1
    L = (list *)malloc(sizeof(node)); Assigning list* to a variable having type list looks weird. Also why the size to allocate is one of node while the buffer seems to be used as list? By the way, what are list and node? Commented May 4, 2020 at 4:45
  • Ah, checking if reading is successful will be required because the loop is 100 times while the file contains only 50 lines. It will be like if (fscanf(fp, "%d%d", &(q -> x), &(q -> y)) != 2) { free(q); break; }. Commented May 4, 2020 at 4:48
  • 1
    Your program will try to read files even after printing Error: unable to open file.... This will be dangerous. Commented May 4, 2020 at 4:50
  • 1
    You need to post the definition of List and node. The handling of List L seems wrong and having both nextx and nexty seems wrong. Normally a linked list only has one "next"-pointer. Commented May 4, 2020 at 5:36

1 Answer 1

1

1) You can use fscanf to read the integer pair from a file. Check the return value to make sure fscanf matched 2 items

2) Your linked list handling is wrong. A normal linked list only has one "next" pointer.

The code can be written in many ways. Here is one example:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct s_node
{
    int x;
    int y;
    struct s_node* next;
} node;

void print_list(node* p)
{
    printf("List contains:\n");
    while (p)
    {
        printf("x=%d y=%d\n", p->x, p->y);
        p = p->next;
    }
}

node* new_node(int x, int y)
{
    node* p = malloc(sizeof *p);
    assert(p);
    p->x = x;
    p->y = y;
    p->next = NULL;
    return p;
}

void free_list(node* p)
{
    while(p)
    {
        node* tmp = p;
        p = p->next;
        free(tmp);
    }
}

node* read_list(FILE* fp)
{
    int x, y;
    node* head = NULL;
    node* last = head;

    // Repeat reading as long as fscanf matches 2 items
    while(fscanf(fp, "%d %d", &x, &y) == 2)
    {
        if (last)
        {
            // Insert at end of list
            last->next = new_node(x, y);
            last = last->next;
        }
        else
        {
            // Special handling for first element in list
            head = new_node(x, y);
            last = head;
        }
    }
    return head;    
}

int main(void) 
{
    FILE* fp = stdin;  // Here input is taken from standard in
                       // Use:
                       //        fp = fopen("points.txt", "r");
                       //        assert(fp);
                       //
                       // to read from a file

    node* head = read_list(fp);

    print_list(head);

    free_list(head);

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.