c - 从文件读取到C中的链表

标签 c list memory dynamic

我必须从具有以下格式的文本文件中读取:

TRMMMYQ128F932D901-SEP-SOQMMHC12AB0180CB8<SEP>Faster Pussy cat-SEP-Silent Night

这是我写入链表然后打印它的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)

typedef struct song {
char *id;
char *songId;
char *artist;
char *title;
struct song *nextSong;
} song;

char *strdup(const char *c)
{
char *dup = malloc(strlen(c) + 1);

if (dup != NULL)
    strcpy(dup, c);

return dup;
}

int main()
{
FILE *fp;
char line[400];
char *item;

song *root = NULL;
song *current = NULL;

int i = 0;

fp = fopen("C:\\Users\\DelicAnte\\Desktop\\unique_tracks.txt", "r");

if (!fp) {
    fprintf(stderr, "Cannot be opened");
}
printf("starting");
while (fgets(line, sizeof(line), fp)) {

    if (root == NULL) {

        root = malloc(sizeof(song));
        /*root->id = strtok(line, "<SEP>");
        root->songId = strtok(NULL, "<SEP>");
        root->artist = strtok(NULL, "<SEP>");
        root->title = strtok(NULL, "<SEP>");*/

        item = strtok(line, "<SEP>");
        root->id = strdup(item);

        item = strtok(NULL, "<SEP>");
        root->songId = strdup(item);

        item = strtok(NULL, "<SEP>");
        root->artist = strdup(item);

        item = strtok(NULL, "\n");
        root->title = strdup(item);

        root->nextSong = NULL;

    }
    else {

        current = root;
        if (current != NULL) {
            while (current->nextSong != NULL) {
                current = current->nextSong;
            }
        }
        /*current = malloc(sizeof(song));
        current->id = strtok(line, "<SEP>");
        current->songId = strtok(NULL, "<SEP>");
        current->artist = strtok(NULL, "<SEP>");
        current->title = strtok(NULL, "<SEP>");*/


        current->nextSong = malloc(sizeof(song));
        current = current->nextSong;

        item = strtok(line, "<SEP>");
        current->id = strdup(item);
        //current->id = malloc(strlen(item) + 1);
        //strcpy(current->id, item);

        item = strtok(NULL, "<SEP>");
        current->songId = strdup(item);
        //current->songId = malloc(strlen(item) + 1);
        //strcpy(current->songId, item);

        item = strtok(NULL, "<SEP>");
        current->artist = strdup(item);
        //current->artist = malloc(strlen(item) + 1);
        //strcpy(current->artist, item);

        item = strtok(NULL, "\n");
        current->title = strdup(item);
        //current->title = malloc(strlen(item) + 1);
        //strcpy(current->title, item);

        current->nextSong = NULL;

        printf("%d\n", i);
        i++;

    }

}

fclose(fp);

current = root;

if (current != NULL) {
    while (current->nextSong != NULL) {
        printf("%s %s\n", current->artist, current->title);
        current = current->nextSong;
    }
    printf("%s %s %s %s\n", current->id, current->songId, current->artist, current->title);
}
else {
    printf("NULA JE");
}


system("pause");
return 0;
}

但我得到了不完整字符串的奇怪输出。

最佳答案

strtok(line, "<SEP>");将查找包含 < 的字符定界符, S , E , P , >

使用 strstr如果您需要搜索 "<SEP>"字符串

否则将分隔符更改为;并定义数据如下:

TRMMMYQ128F932D901;SOQMMHC12AB0180CB8;Faster Pussy cat;Silent Night

您的链表重复相同的代码。您可以简化如下并添加错误处理:

char *sep = ";";
while(fgets(line, sizeof(line), fp)) 
{
    current = malloc(sizeof(song));
    current->nextSong = NULL;

    item = strtok(line, sep);
    if(!item) break;
    current->id = strdup(item);
    item = strtok(NULL, sep);
    if(!item) break;
    current->songId = strdup(item);
    item = strtok(NULL, sep);
    if(!item) break;
    current->artist = strdup(item);
    item = strtok(NULL, sep);
    if(!item) break;
    current->title = strdup(item);

    if(root)
        current->nextSong = root;
    root = current;
}
fclose(fp);

此外,当打印代码时遍历列表并打印每个有效元素,如下所示:

if(root) 
{
    current = root;
    while(current != NULL)
    {
        printf("%s %s %s %s\n", 
          current->id, current->songId, current->artist, current->title);
        current = current->nextSong;
    }
}

此代码不会释放任何已分配的内存。您可以在下一步中这样做。

关于c - 从文件读取到C中的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47895331/

相关文章:

C 扫描输入字符串保存到错误的变量

python - 如果列表位于其他列表内的 bool 表达式

c++ - "New"运算符的不同行为 - 动态内存分配

c - 在数组 : Segmentation fault (core dumped) 中插入一个元素

c - 在 C 中读取字符串的详细信息?

r - 使用 R 在 for 循环中创建具有不同名称的向量

SwiftUI 更新导航操作的状态

ios - 通过数据创建 UIImage 时崩溃

jquery - jQPLOT - 内存泄漏

c - C 中基本基数特里树的实现