读.csv文件放到C的LinkedList [英] Reading .csv file into C LinkedList

查看:182
本文介绍了读.csv文件放到C的LinkedList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下的结构

 结构NODE {
    字符的用户名[50];
    字符密码[50];
    字符的用户类型[50];
    结构NODE *接下来的;
} *头= NULL;

我想从的.csv 文件,比如 database.csv 形式<$ C阅读$ C>用户名,密码,用户类型,记号化每行到使用标记 strtok的,并把每个令牌右场里面。例如,我的文件看起来是这样的:

强尼·戴普,pirate123,用户结果
tonystark,iron456,系统操作员

我继续读第C 的LinkedList ,但我不能弄明白。任何帮助将大大AP preciate,还是在C对如何实施的LinkedList 任何好的引用。

我的主要问题是把元素在每一个节点。我知道如何使用 strtok的来标记文件中的一条线。这是我迄今所做的:

 无效createList(){
    FILE *的数据;
    数据=的FileOpen(password.csv,R);
    焦炭parsedLine [50];
    而(与fgets(parsedLine,50,数据)!= NULL){
    字符* PTR =的strtok(parsedLine,);
        节点*温度;
        TEMP =(节点*)malloc的(的sizeof(节点));
    //我在这里停留//
}

谢谢!

修改

将这项工作?

 的extern无效createList(){FILE *的数据;
数据=的FileOpen(password.csv,R);
焦炭parsedLine [50];
而(与fgets(parsedLine,50,数据)!= NULL){
    结构NODE *节点=的malloc(sizeof的(结构NODE));
    字符*的getUser = strtok的(parsedLine,);
    的strcpy(与于节点GT;用户名,的getUser);
    字符*将getpass =的strtok(NULL,);
    的strcpy(与于节点GT;的密码,将getpass);
    字符*的getType =的strtok(NULL,);
    的strcpy(与于节点GT;用户类型,的getType);
    于节点&gt;接下来=头;
    头=节点;
}
FCLOSE(数据);}


解决方案

这其实很简单...你有一个节点结构,其中包含了接下来指针,一个变量指向头(第一个节点)的列表。头指针开始作为 NULL 意思列表是空的。

要添加您创建一个节点一个节点,然后将新创建的节点接下来指针指向列表的现任掌门人,并设置头指向新节点:

  / *分配新的节点* /
结构NODE *节点=的malloc(sizeof的(结构NODE));/ *链接到当前的头* /
于节点&gt;接下来=头;/ *将新节点列表的头部* /
头=节点;

一旦这样做了以后,你有一个包含一个节点列表。做两次后你有一个两节点列表。等等。

I have the following struct:

struct NODE {
    char username[50];
    char password[50];
    char usertype[50];
    struct NODE *next;
} *head=NULL;

I would like to read from a .csv file, say database.csv of the form username, password, usertype, tokenize each line into tokens using strtok and put each token inside the right field. For instance, my file looks like this:

johnnydepp, pirate123, user
tonystark, iron456, sysop

I keep reading on C LinkedList, but I cannot figure it out. Any help would be greatly appreciate, or any good references on how to implement a LinkedList in C.

My main problem is putting element in the each of the node. I know how to use strtok to tokenize a line in a file. This is what I have done so far:

void createList() {
    FILE *data;
    data = fileopen("password.csv", "r");
    char parsedLine[50];
    while (fgets(parsedLine, 50, data) != NULL) {
    char *ptr = strtok(parsedLine, ", ");
        node *temp;
        temp = (node*)malloc(sizeof(node));
    // I am stuck here //
} 

Thanks!

EDIT

Will this work?

extern void createList() {

FILE *data;
data = fileopen("password.csv", "r");
char parsedLine[50];
while (fgets(parsedLine, 50, data) != NULL) {
    struct NODE *node = malloc(sizeof(struct NODE));
    char *getUser = strtok(parsedLine, ", ");
    strcpy(node->username, getUser);
    char *getPass = strtok(NULL, ", "); 
    strcpy(node->password, getPass);
    char *getType = strtok(NULL, ", ");
    strcpy(node->usertype, getType);    
    node->next = head;
    head = node;
}
fclose(data);

}

解决方案

It's actually very simple... You have a NODE structure, which contains a next pointer, and a variable head which points to the head (first node) of the list. The head pointer starts out as NULL meaning the list is empty.

To add a node you create a node, then set the newly created nodes next pointer to point to the current head of the list, and set the head to point to the new node:

/* Allocate new node */
struct NODE *node = malloc(sizeof(struct NODE));

/* Link to the current head */
node->next = head;

/* Make the new node the head of the list */
head = node;

After doing this once, you have a list containing one node. After doing it twice you have a two-node list. Etc.

这篇关于读.csv文件放到C的LinkedList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆