一、fseek
fseek是设置文件指针偏移量的函数,具体传参格式为:
int fseek(FILE *stream, long int offset, int whence)
返回一个整数,其中:
1、stream是指向文件的指针
2、offset是偏移量,一般是指相对于whence的便宜量
3、whence这是表示开始添加偏移 offset 的位置。它一般指定为下列常量之一:
常量 | 描述 |
SEEK_SET | 文件开头 |
SEEK_CUR | 文件指针当前位置 |
SEEK_END | 文件的末尾 |
返回值:
成功则返回0,失败则返回非零
用法:
#include <stdio.h>
int main ()
{
FILE *fp;
fp = fopen("file.txt","w+");
fputs("hello", fp);
fseek( fp, 6, SEEK_SET );
fputs("world", fp);
fclose(fp);
return(0);
}
执行之后文件内容为:
hello world