【问题标题】:How to read a csv file into a structure linked list in c如何将csv文件读入c中的结构链表
【发布时间】:2020-05-05 03:09:52
【问题描述】:

我是新来这里和一般的编程(请注意管理员和编程大师让我轻松,谢谢)我正在用 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("===========================================");

}

output of the program

【问题讨论】:

    标签: c csv struct linked-list structure


    【解决方案1】:

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

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

    一旦你添加了第一个节点,你每次迭代tail-&gt;next指向你创建的新节点,并使tail等于新节点。


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

    【讨论】:

      猜你喜欢
      • 2013-12-16
      • 2019-01-31
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 2013-12-11
      • 2021-02-13
      相关资源
      最近更新 更多