简体   繁体   中英

How to read a csv file into a structure linked list in c

I am new here and to programming in general ( note to admins and the gurus of programming to go easy on me, thanks) and i am doing a homework for school in c about a small program that reads a csv file into a singly linked list where the data is a structure, then displays it, and sorts it and writes it out to a text file etc..

the problem i am having now is either with the read function or the display function : the result is that the data is either being read or displayed in the reverse order and one line is shifted down..

i have banged my head for a while on it, but now i am running out of time and i thought to ask it here, maybe to get some feedback from fresh eyes. Attached as a link is a screenshot of the contents of the file to read and the output of the program (apparently because i am a new user, i cant upload the photo directly to the site..) thanks in advance

here are the relevant lines of code :

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <conio.h>
#include <string.h>


// DEFINE
#define CSV_FILE_TO_READ "TPP_TP_Data_2019_base.csv"


// ============================
// GLOBAL VARIABLES


struct node
{
    char Name[50];
    char Firstname[50];
    char Initials[10];
    char Mobile[30];
    char Class[50];
    char InitialSort[50]; // change into int
    char RandomSort[50];  // change into float

    struct node *next;
} *head;


// ============================
// FONCTION PROTOTYPE DECLARATIONS

void Read();
void Display();


// ============================
// MAIN

int main()
{

    Read();
    Display();


    return 0;
}

// ============================
// FUNCTIONS

void Read()
{

    FILE *fPointer;
    fPointer = fopen(CSV_FILE_TO_READ,"r");

    if (fPointer == NULL)
    {
        printf("\nCould not open file %s",CSV_FILE_TO_READ);
        return;
    }

    //reading the file and creating liked list

    char parsedLine[100];
    while(fgets(parsedLine, 100, fPointer) != NULL)
    {
        struct node *node = malloc(sizeof(struct node));

        char *getName = strtok(parsedLine, ";");
        strcpy(node->Name, getName);

        char *getFirstname = strtok(NULL, ";");
        strcpy(node->Firstname, getFirstname);

        char *getInitials = strtok(NULL, ";");
        strcpy(node->Initials, getInitials);

        char *getMobile = strtok(NULL, ";");
        strcpy(node->Mobile, getMobile);

        char *getClass = strtok(NULL, ";");
        strcpy(node->Class, getClass);

        char *getInitialSort = strtok(NULL, ";");  // change function into int getter
        strcpy(node->InitialSort, getInitialSort);

        char *getRandomSort = strtok(NULL, ";");  // change function into a float getter
        strcpy(node->RandomSort, getRandomSort);


        node->next = head;
        head = node;

    }

    fclose(fPointer);
}




void Display()  // displays the content of the linked list
{
    struct node *temp;
    temp=head;
    while(temp!=NULL)
    {
        printf("%s %s %s %s %s %s %s \n",temp->Name,temp->Firstname,temp->Initials,temp->Mobile,temp->Class,temp->InitialSort,temp->RandomSort);
        temp = temp->next;
    }
    printf("\n");
    printf("===========================================");

}

output of the program

The way you create your list is as a stack , because you add each new node at the head of the list. To get the correct order you need to append at the end (tail) of the list.

You can do that by keeping track of the last node in the list (you only need it in the Read function). Initially head and tail are equal, if there only one node in the list both the head and the tail of the list would be the same.

Once you have added the first node, each iteration you make tail->next point to the new node you have created, and make tail equal to the new node.


If you're unsure about its operations I suggest you use a pen and some papers and use it to draw the list and all operations on it. Use squares as nodes, and arrows as links (or pointers in general).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM