简体   繁体   English

如何将csv文件读入c中的结构链表

[英]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..我是新来的,一般来说是编程(请注意管理员和编程大师对我很轻松,谢谢),我正在用 c 为学校做作业,关于一个小程序,该程序将 csv 文件读入单链表其中数据是一个结构,然后显示它,并对其进行排序并将其写入文本文件等。

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).您可以通过跟踪列表中的最后一个节点来做到这一点(您只需要在Read函数中使用它)。 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.最初的headtail是相等的,如果链表中只有一个节点,则链表的头和尾将是相同的。

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.添加第一个节点后,每次迭代都会使tail->next指向您创建的新节点,并使tail等于新节点。


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).使用正方形作为节点,使用箭头作为链接(或一般的指针)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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