gpt4 book ai didi

c - 从文件中读取数据到链表中

转载 作者:行者123 更新时间:2023-11-30 15:43:39 25 4
gpt4 key购买 nike

嘿,我这里有一段代码,应该将文件读入链接列表:

void loadFile() {
Event *event;
FILE *file;
int nRead = 7;
char filename[30];
LinkedList *list;

list = (LinkedList*)malloc(sizeof(LinkedList));
event = (Event*)malloc(sizeof(Event));

printf("Please input filename\n");
scanf("%s", filename);
file = fopen(filename, "r");

nRead = fscanf(file, "%d-%d-%d %d:%d %d %s", &(list->head->event->date->year), &(list->head->event->date->month), &(list->head->event->date->day), &(list->head->event->time->hours), &(list->head->event->time->minutes), &(list->head->event->duration), event->name);

if(nRead != 7) {
printf("Error in reading file.\n");
}

fclose(file);
}

这里是我的主要内容的一部分

int main(int argc, char **argv) { 
Window *window;
Event *event;
FILE *file;

window = createWindow("Calendar");

if(argc == 2) {
file = fopen(argv[1], "r");
if(file == NULL) {
printf("Could not open file.\n");
} else {
printf("File opened successfully!\n");
}
}
else {
addButton(window, "Load a calendar from file.", *loadFile(), &event); //ERROR HERE
}
}

这里是addButton(不需要了解功能,只需要语法帮助)

void addButton(Window *window, char *label, void (*callback)(void*), void *data);

我收到错误:

assignment.c: In function ‘main’:
assignment.c:22: error: void value not ignored as it ought to be

我真的不知道为什么,我被困在这部分了。我环顾四周,发现当您将带有 void 返回值的函数分配给变量或其他东西时,会发生 void value notignored 错误,但我不认为我在代码中这样做...

如有任何帮助,我们将不胜感激

最佳答案

将代码更改为:

addButton(window, "Load a calendar from file.", loadFile, &event);

您传递的是 *loadFile(),它实际上调用 loadFile 函数,然后尝试取消引用 void 返回值。您想要做的是将指针传递给该函数。

仍然存在一个小问题,因为 addButton 需要 void (*callback)(void*),您应该传递一个具有单个 void* 参数的 void 函数。但 loadFile 不匹配。要解决此问题,您应该声明 loadFile 以接受 void* 参数。

void loadFile( void *data ) {
/* etc... */
}

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

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