从文件读取到C中的链表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从文件读取到C中的链表相关的知识,希望对你有一定的参考价值。
我必须从具有以下格式的文本文件中读取:
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, "
");
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, "
");
current->title = strdup(item);
//current->title = malloc(strlen(item) + 1);
//strcpy(current->title, item);
current->nextSong = NULL;
printf("%d
", i);
i++;
}
}
fclose(fp);
current = root;
if (current != NULL) {
while (current->nextSong != NULL) {
printf("%s %s
", current->artist, current->title);
current = current->nextSong;
}
printf("%s %s %s %s
", 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
",
current->id, current->songId, current->artist, current->title);
current = current->nextSong;
}
}
此代码不释放任何分配的内存。你可以为下一步做到这一点。
以上是关于从文件读取到C中的链表的主要内容,如果未能解决你的问题,请参考以下文章