繁体   English   中英

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

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

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

我现在遇到的问题是读取功能或显示功能:结果是数据要么被读取,要么以相反的顺序显示,一行向下移动..

我已经在这上面撞了一段时间,但现在我的时间不多了,我想在这里问一下,也许是为了从新人那里得到一些反馈。 作为链接附加的是要读取的文件内容和程序输出的屏幕截图(显然因为我是新用户,我无法将照片直接上传到网站..)在此先感谢

这是相关的代码行:

#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("===========================================");

}

程序的输出

创建列表的方式是堆栈,因为您将每个新节点添加到列表的头部。 要获得正确的顺序,您需要在列表的末尾(尾部)附加。

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

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


如果您不确定它的操作,我建议您使用笔和一些纸,并用它来绘制列表和所有操作。 使用正方形作为节点,使用箭头作为链接(或一般的指针)。

暂无
暂无

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

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