Thread: Read file into linked list

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    18

    Read file into linked list

    hello guys i tried to read file into linked list and then print it
    but when i print it the first sentences is deleted .
    what am i doing wrong?

    Code:
    int read_file(char * filename) {	int i = 0;
    	int a[20], b[20];
    	FILE *fp;
    	char line[128];
    	LIST *current, *head;
    	head = current = NULL;
    	fopen_s(&fp, filename, "r");
    	if (fp == NULL)
    		return 0;
    	while (fgets(line, sizeof(line), fp)) {
    		LIST *node = malloc(sizeof(LIST));
    		node->string = strdup(line);
    		node->next = NULL;
    		if (head == NULL) {
    			current = head = node;
    		}
    		else {
    				current = current->next = node;
    				char str[20];
    				char* field = strtok(line, ",");
    				strcpy(str, field);
    				a[i] = (double)atof(str);
    				field = strtok(NULL, ",");
    				b[i] = (double)atof(field);
    				i++;
    			}
    		}
    	fclose(fp);
    	printf("Thermic table\nx       y\n======  ======\n");
    	for (int k = 0; k < 13; k++)
    		printf("%d  %d\n", a[k], b[k]);
    	
    	printf("\n--------------------------\nLets fly\n--------------------------");
    	free_list(head);
    	return 1;
    my output:15000 8500
    1000 10000
    19000 20100
    35000 40100
    23000 30000
    25000 42000
    300 19000
    3000 30000
    6000 25000
    6000 40000
    15000 43000
    30000 15555

    the real output :
    5700,5000
    15000,8500
    1000,10000
    19000,20100
    35000,40100
    23000,30000
    25000,42000
    300,19000
    3000,30000
    6000,25000
    6000,40000
    15000,43000
    30000,15555

  2. #2
    Registered User
    Join Date
    Mar 2020
    Posts
    18
    Quote Originally Posted by cbeginner12334 View Post
    hello guys i tried to read file into linked list and then print it
    but when i print it the first sentences is deleted .
    What am i doing wrong?

    Code:
    int read_file(char * filename) {    int i = 0;
        int a[20], b[20];
        file *fp;
        char line[128];
        list *current, *head;
        head = current = null;
        fopen_s(&fp, filename, "r");
        if (fp == null)
            return 0;
        while (fgets(line, sizeof(line), fp)) {
            list *node = malloc(sizeof(list));
            node->string = strdup(line);
            node->next = null;
            if (head == null) {
                current = head = node;
            }
            else {
                    current = current->next = node;
                    char str[20];
                    char* field = strtok(line, ",");
                    strcpy(str, field);
                    a[i] = (double)atof(str);
                    field = strtok(null, ",");
                    b[i] = (double)atof(field);
                    i++;
                }
            }
        fclose(fp);
        printf("thermic table\nx       y\n======  ======\n");
        for (int k = 0; k < 13; k++)
            printf("%d  %d\n", a[k], b[k]);
        
        printf("\n--------------------------\nlets fly\n--------------------------");
        free_list(head);
        return 1;
    my output:15000 8500
    1000 10000
    19000 20100
    35000 40100
    23000 30000
    25000 42000
    300 19000
    3000 30000
    6000 25000
    6000 40000
    15000 43000
    30000 15555

    the real output :
    5700,5000
    15000,8500
    1000,10000
    19000,20100
    35000,40100
    23000,30000
    25000,42000
    300,19000
    3000,30000
    6000,25000
    6000,40000
    15000,43000
    30000,15555
    help.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,178
    What do likes 19 to 25 have to do with your linked list?

    What does the loop at line 30 have to do with your linked list?

    > free_list(head);
    That's a good idea.

    So apply the same idea and make something like
    Code:
        while (fgets(line, sizeof(line), fp)) {
            head = add_to_list(head, line);
    Your function has a meandering purpose, factor out things which are not specific to the act of reading data from the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    18
    Quote Originally Posted by Salem View Post
    What do likes 19 to 25 have to do with your linked list?

    What does the loop at line 30 have to do with your linked list?

    > free_list(head);
    That's a good idea.

    So apply the same idea and make something like
    Code:
        while (fgets(line, sizeof(line), fp)) {
            head = add_to_list(head, line);
    Your function has a meandering purpose, factor out things which are not specific to the act of reading data from the file.
    line 19-25 need to convert for 2 arrays (i dont know how to do it with linked list)
    line 30 print the linked list (?)
    i did not understand what is the mean of the function add_to_list
    can you explain me ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,178
    > i did not understand what is the mean of the function add_to_list
    > can you explain me ?

    It means take this code, and move it into a separate function called add_to_list
    Code:
            list *node = malloc(sizeof(list));
            node->string = strdup(line);
            node->next = null;
            if (head == null) {
                current = head = node;
            }
            else {
                    current = current->next = node;
    > line 19-25 need to convert for 2 arrays (i dont know how to do it with linked list)
    It would be better just to say that.


    Code:
    int read_file(char * filename) {
        FILE *fp;
        char line[128];
        LIST *head = NULL;
        fopen_s(&fp, filename, "r");
        if (fp == NULL)
            return 0;
        while (fgets(line, sizeof(line), fp)) {
            // extract the useful information from line, and append a
            // suitably allocated LIST element containing that information
            // to the end of the list identified as 'head'.
            head = add_to_list(head, line);
        }
        fclose(fp);
        printf("thermic table\nx       y\n======  ======\n");
        print_list(head);
        free_list(head);
        return 1;
    }
    What does your 'LIST' typedef look like?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to read a .txt file into a linked list.
    By AMark in forum C Programming
    Replies: 2
    Last Post: 09-15-2014, 11:25 PM
  2. Read text file into a linked list
    By riddinon24z in forum C Programming
    Replies: 38
    Last Post: 08-04-2011, 01:49 PM
  3. read/write linked list
    By lambs4 in forum C Programming
    Replies: 4
    Last Post: 03-29-2003, 06:38 PM
  4. read file into Linked list, please
    By unhwan in forum C Programming
    Replies: 3
    Last Post: 06-11-2002, 07:18 PM
  5. How to read a file to a linked list?
    By c beginner in forum C Programming
    Replies: 1
    Last Post: 04-09-2002, 10:59 AM

Tags for this Thread