代码示例_标准IO_fseek

fseek


fseek.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main(void)
 5 {
 6     // 打开/创建
 7     FILE *fp = fopen("./1.text","w+");
 8     if(fp==NULL){
 9         perror("fopen failed");
10         exit(1);
11     }
12 
13     // 写字符串
14     fputs("panda_w",fp);
15 
16 
17     // 文件定位
18     fseek(fp,2,SEEK_SET);      //将文件流位置置开头,并且移动两位!
19 
20 
21     // 读字符
22     int p = fgetc(fp);
23     if(p==EOF){
24         perror("fgetc failed");
25         exit(1);
26     }
27     printf("read : %c
",p);
28     
29 
30     // 关闭
31     fclose(fp);
32 
33     return 0 ;
34 }

测试:


success !

Stay hungry, stay foolish 待续。。。
原文地址:https://www.cnblogs.com/panda-w/p/11076064.html