C语言读取txt文件
使用vs2019时用fscanf_s读取txt文件内容,发现读取第一行第一个数据时出现读取不到的情况。下面是读取数据的函数。
stu* readdate(char* filename) {
char s = 0;
FILE* fp = fopen(filename, "rb");
stu * p= NULL;
stu * q = NULL;
stu * head = NULL;
if (fp != NULL) {
for (;(s=getc(fp) )!=EOF; ) {
q = (stu*)malloc(sizeof(stu));
if (q != NULL) {
fscanf_s(fp, "%d", &q->id);//这里
fscanf_s(fp, "%s", q->name,20);
fscanf_s(fp, "%d", &q->old);
fscanf_s(fp, "%s", q->sex,5);
fscanf_s(fp, "%f", &q->yu);
fscanf_s(fp, "%f", &q->shu);
fscanf_s(fp, "%f", &q->wai);
fscanf_s(fp, "%f", &q->li);
q->next = NULL;
if (head == NULL) {
head = q;
}
else if (p != NULL) {
p->next = q;
}
p = q;
}
q = (stu*)malloc(sizeof(stu));
}
}
return head;
}//信息读入
输入
id:1
读取
id:-842150451
输入
id:123456
读取
id:23456
只有第一组数据出现这种情况,后面输入的都没有这种情况