简体   繁体   English

我需要将文件“Mus​​icLibrary.txt”中的数据复制到结构。 但是当我使用我的代码时,它不会返回任何内容

[英]I need to copy data from a file, “MusicLibrary.txt” to a strucutre. But when I'm using my code, it doesn't return anything

Here's my code for copying the file. 这是我复制文件的代码。

song_t *ReadFile(song_t *head){
FILE *input;
input = fopen("MusicLibrary.txt", "r");
song_t *temp = head;
string title;
string artist;
string album;
string genre;
string store;
string buffer;

while(fgets(buffer, MAXSIZE, input) != NULL){
    temp = (song_t *)malloc(sizeof(song_t));
    fgets(temp->title, MAXSIZE, input);
    fgets(temp->artist, MAXSIZE, input);
    fgets(temp->album, MAXSIZE, input);
    fgets(temp->genre, MAXSIZE, input);
    fgets(store, MAXSIZE, input);
    temp->rating = atof(store);
    temp->next == NULL;
    temp = temp->next;
}

fclose(input);
return head;
}

and here is the code for my structre: 这是我的structre的代码:

typedef struct song{
string title;
string artist;
string album;
string genre;
float rating;
struct song *next;
}song_t;

also, typedef char string[30]; 另外,typedef char string [30];

i can't seem to copy the data from files to the structure in main. 我似乎无法将数据从文件复制到main中的结构。 Can anyone help me with this? 谁能帮我这个?

while(fgets(buffer, MAXSIZE, input) != NULL){
    temp = (song_t *)malloc(sizeof(song_t));
    fgets(temp->title, MAXSIZE, input);
    fgets(temp->artist, MAXSIZE, input);
    fgets(temp->album, MAXSIZE, input);
    fgets(temp->genre, MAXSIZE, input);
    fgets(store, MAXSIZE, input);
    temp->rating = atof(store);
    temp->next == NULL;
    temp = temp->next;
}

There are a few problems with the above code that may not be problems depending on you input file format, but here goes: 上面的代码有一些问题可能不是问题,具体取决于您输入的文件格式,但这里有:


First, you control the while loop with a fgets that throws a line away. 首先,你用一个抛出一条线的fgets来控制while循环。 Are you certain that's what you wanted. 你确定这是你想要的吗?


Secondly, you should always check the return value from malloc in case it fails. 其次,您应该始终检查malloc的返回值,以防它失败。


Thirdly, you're assuming that each field is on its own line. 第三,你假设每个领域都是独立的。 You should confirm that. 你应该确认一下。


Fourth, you're not really inserting correctly into a linked list. 第四,你并没有真正插入到链表中。 If your intent is to insert new items at the start of the list, change: 如果您打算在列表的开头插入新项目,请更改:

temp->next == NULL;
temp = temp->next;

into: 成:

temp->next == head;
head = temp;

and ensure you call it as: 并确保将其称为:

actualHead = ReadFile (actualHead);

to update the head pointer correctly. 正确更新head指针。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的代码没有在文件(txt)中写入任何内容 - Why my code doesn't write anything in file (txt) 此代码不返回 output。我需要帮助 - This code doesn't return output. I need help please 从文件读取数据时,为什么我的函数没有存储我想要的值? - Why doesn't my function store the values I want it to when reading data from a file? 我需要复制字符串,但它不起作用 - I need to copy string, but it doesn't work 我应该从.txt中读取文本并将其显示在控制台中,但是我的代码无法正常工作,我的错误在哪里? - I'm supposed to read a text from a .txt and display it in the console but my code is not working,where is my mistake? 使用Mac Terminal Utility时,printf不返回任何内容 - printf doesn't return anything when using the Mac Terminal Utility 如何使用读取功能读取文件时,数据不会被切断 - How can I make it so my data doesn't get cut off when reading a file using the read function 有人可以帮我解决 C 问题吗? 当我尝试运行它不返回任何东西 - Could someone help me with that C problem? When I try to run it doesn't return anything 从文件中读取不会返回任何内容,但会被访问 - Reading from file doesn't return anything, but it is accessed 当我在 C 代码中包含 scanf() 时,它不起作用 - When I include scanf() in my C code it doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM