簡體   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