gpt4 book ai didi

c - fscanf 函数不会在 c 中读取

转载 作者:太空宇宙 更新时间:2023-11-04 08:26:48 24 4
gpt4 key购买 nike

我尝试从文件中读取一些数据并将其插入到队列中,插入功能运行良好,并且我尝试使用 printfs 捕获错误。我看到在 while() 行中有错误。文件中的数据是这样的

12345 2

11232 4

22311 4

22231 2

void read_file(struct Queue *head){
FILE *fp;
int natid;
int cond;
fp=fopen("patients.txt","r");

while (fscanf(fp,"%d %d", natid, cond) != EOF)
insert(head,natid,cond);

fclose(fp);}

最佳答案

您必须将指针传递给fscanf() 存储值的位置,并检查所有预期的转换是否成功:

while (fscanf(fp, "%d %d", &natid, &cond) == 2)

关于c - fscanf 函数不会在 c 中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30125846/

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