简体   繁体   English

使用fread()读取多个记录

[英]Using fread() to read multiple records

Following is my code for inserting data into file 以下是我将数据插入文件的代码

Note: I have one structure named record for that 注意:我有一个名为record结构

fwrite(&record, sizeof(record),1,fptr);

I use the above code in a loop, to enter multiple records. 我在循环中使用以上代码,以输入多个记录。 Now, I have my data reading program as this: 现在,我有了这样的数据读取程序:

do {
fread(&record, sizeof(record), 1, fptr);
printf("\nName: %s \nAddress: %s \nClass Level: %d \nTelepone: %ld",record.name, record.address, record.classlevel, record.telephone);
}while (feof(fptr));

But it only displays only first record. 但是它仅显示第一条记录。

I also tried using fseek() for that as follows: 我也尝试如下使用fseek()

do {
fread(&record, sizeof(record), 1, fptr);
printf("\nName: %s \nAddress: %s \nClass Level: %d \nTelepone: %ld",record.name, record.address, record.classlevel, record.telephone);
fseek(fptr,sizeof(record)+1, SEEK_SET);
}while (feof(fptr));

And Still It didn't work!! 仍然没有用! I am trying to do an small project on C and This File Handling is just getting on my nerves. 我正在尝试在C上做一个小项目,这个文件处理让我感到不安。

change this: 改变这个:

while (feof(fptr))

to this: 对此:

while (!feof(fptr))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM