C 语言增删查改_光标移动的用法实例文档说明
Author :罗成
QQ : 905657420
C 语言中指针也就是光标移动的函数是:fseek(指针名,偏移量,状态量);
如: FILE *fptr;
Fseek(fptr,offset,SEEK_SET);
状态量分为:SEEK_SET(开始位置);SEEK_CUR(当前位置);SEEK_END(末尾位置)。
如下代码是简单的操纵指针移动:
printf("Enter the offset !");
scanf("%d",&offset);
fseek(fptr,offset,SEEK_SET);
fread(&rec,sizeof(rec),1,fptr);
printf("%d,%s",res->age,res->name);
删除,修改都是在光标移动的基础上进行的。确定数据的地址是关键。因此,我就写了一
个查询数据地址的方法,方便其他方法的调用:
int search_data(char * str) // Search data function(Module)
{
FILE *fptr; 创建文件指针
int offset,i=0; 定义指针偏移量 OFFSET
person rec; 结构体的引用
fptr=fopen("record.bin","r+b"); 文件只读可修改
if(fptr==NULL)
printf("Could not open the file !");
else
{
printf("——————————————");
while(!feof(fptr)) 循环读取
{
fread(&rec,sizeof(rec),1,fptr); // If query the name(查找姓名)
if(strcmp(rec.w_name,str)==0)
{
offset =sizeof(rec)*i; 找到相匹配的姓名,记录下该数据的地址
break; 跳出
}
else if(strcmp(rec.w_tel,str)==0) // If query the telephone number (查找电话号码)