从文件中读取C

时间:2017-12-19 21:18:59

标签: 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;
}

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

1 个答案:

答案 0 :(得分:0)

strtok(line, "<SEP>");会查找包含<SEP>

的字符分隔符

如果您需要搜索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;
    }
}

此代码不释放任何已分配的内存。你可以为下一步做到这一点。