【发布时间】: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("===========================================");
}
【问题讨论】:
标签: c csv struct linked-list structure