无涯教程-C fseek() example函数

53 阅读1分钟

fseek()函数用于将文件指针设置为指定的偏移量。它用于将数据写入所需位置的文件。

语法:

int fseek(FILE *stream, long int offset, int whence)

在fseek()函数中使用了3个常量,它们分别是SEEK_SET,SEEK_CUR和SEEK_END。

示例:

#include<stdio.h>
void main(){
   FILE *fp;

   fp = fopen("myfile.txt","w+");
   fputs("This is learnfk", fp);
  
   fseek( fp, 7, SEEK_SET );
   fputs("sonoo jaiswal", fp);
   fclose(fp);
}

myfile.txt

This is sonoo jaiswal

参考链接

www.learnfk.com/c-programmi…