目录
四、文件的随机读写
1.fseek---根据文件指针的位置和偏移量来定位文件指针
int fseek ( FILE * stream , long int offset , int origin )
offset---偏移量
origin---指针位置
SEEK_CUR(文件指针当前的位置)
SEEK_END(文件末尾的位置)
SEEK_SET(文件开始的位置)
int main()
{
//打开文件
FILE* pf = (FILE*)fopen("test.hex", "w");
if (pf == NULL)
{
perror("fopen");
return;
}
//写文件
fputs("ABCD*************", pf);
fseek(pf,8, SEEK_SET);//pf指针指向从首位置向后偏移8个字符的位置
fputs("EFGH", pf);
fseek(pf, 8, SEEK_END);//pf指针指向从末尾向后偏移8个字符的位置
fputs("amazing", pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
2.ftell-----返回文件指针相对于起始位置的偏移量