gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:19:44 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com