c - 为什么我的 addToList 方法不能正常工作?

标签 c string file list text-files

我是新来的,这是我的第一个问题:

这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct Person{
        char *vorname;
        char *nachname;
};
typedef struct Person Person;

struct Node{
        Person *data;
        struct Node *next;
};
typedef struct Node Node;
void addToList(Person *p);
void readFile();
Node *start = NULL;

void readFile(){
        FILE *f;
        Person *p;
        char input[501];
        char *line;
        f = fopen("persdat.txt", "r");
        while(fgets(input, 500, f) != NULL){
                line = strtok(input, ";");

                strcpy(p->vorname, line);

                line = strtok(NULL, ";");

                strcpy(p->nachname, line);
                printf("%s wurde eingelesen.\n", p->vorname);
                addToList(p);
                line[0] = '\0';
        }
}
void addToList(Person *p){
        Node *newNode = malloc(sizeof(Node));
        newNode->data = p;
        newNode->next = NULL;
        if (start == NULL){
                start = newNode;
                printf("%s wurde in die Liste eingefügt. (1. Stelle)\n", newNode->data->vorname);
        }else{
                Node *current = start;
                int stelle = 2;
                while(current->next != NULL){
                        printf("Liste wird durchlaufen... aktuelle Person: %s\n", current->data->vorname);
                        current = current->next;
                        stelle++;
                }
                current->next = newNode;
                printf("%s wurde in die Liste eingefügt (%d. Stelle).\n", newNode->data->vorname, stelle);
        }
}
void output(){
        Node *current = start;
        printf("%s\n", current->data->vorname);
    do{
                current = current->next;
                printf("%s\n", current->data->vorname);
                }while (current->next != NULL);

}

int main(int argc, char *argv[]) {

        printf("\n");
        readFile();
        output();
        return EXIT_SUCCESS;
}

结构体Person具有成员char *vornamechar *nachname。 struct Node 具有成员 Person *data 和 struct Node *next (我已键入定义)。 start 是声明的列表。

这是我正在使用的文本文件:

Daniel;Smith
Florian;Mayne
Michael;Fabour
Manuel;Spoley
Andrea;Bree
Prapot;BumBum
Tobias;Sinkleeser
Nikolaus;Muffed

终端输出:

enter image description here

函数output()打印8次Nikolaus,而我希望它输出我插入列表中的8个名字,这是为什么?

最佳答案

readFile() 中,您缺少为 struct Person 以及 struct Person 的成员分配任何内存.

所以如下

void readFile(){
    FILE *f;
    Person *p;
    char input[501];
    char *line;
    f = fopen("persdat.txt", "r");
    while(fgets(input, 500, f) != NULL){
            line = strtok(input, ";");

            strcpy(p->vorname, line);
            ...

看起来可能更好:

void readFile(){
    FILE *f;
    Person * p = calloc(1, sizeof(*p));
    char input[501];
    char *line;
    f = fopen("persdat.txt", "r");
    while(fgets(input, 500, f) != NULL){
            line = strtok(input, ";");

            p->vorname = strdup(line);
            ...

(为了可读性省略了错误检查)

关于c - 为什么我的 addToList 方法不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16751831/

相关文章:

Python - 从电子邮件附件下载 excel 文件然后解析它

java - 为什么 TextToSpeech SynthesizeToFile 返回 -1?

c - <template> 替换 C 链表

objective-c - cgo godefs 和 Objective-C

控制台不显示 Eclipse 中的输出

c - 在 C 中,当字符串被空字符截断时会发生什么?

python - 如何访问 Python 字符串中的第一个字符和整数对?

javascript - JS : write a join function using reduce method . .. 函数的控制台日志返回不正确的输出

Java 字符串 : The output of the program is not as expected

c# - 删除txt文件中的回车符