c語言文件指針函數——ftell和fseek()


  ftell()函數

在c語言中,ftell函數用來返回當前文件指針的位置。定義在stdio.h頭文件中。

函數原型:long ftell(FILE *fp)
返回當前文件指針位置。這個位置是當前文件指針相對於文件開頭的位移量。

返回值:返回文件指針的位置,若出錯則返回-1L
實例:

 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     FILE *fp;
 5     fp = fopen("test.txt", "W+");
 6     /*按照格式要求將字符串寫入文件*/
 7     fprintf(fp, "This is a test");
 8     /*讀出文件指針fp的位置*/
 9     printf("The file pointer is at byte %ld\n", ftell(fp));
10     fclose(fp);
11     return 0;
12 }

注意:字符串共有14個字符,地址為0~13.調用fprintf函數后,文件指針自動移到讀入的最后一個字符的下一個位置。本例中就是文件的結束符,它的地址是14.

fseek()函數

在閱讀代碼時,遇到了很早之前用過的fseek(),很久沒有用了,有點陌生,寫出來以便下次查閱。

函數功能是把文件指針指向文件的開頭,需要包含頭文件stdio.h

fseek
  函數名: fseek
  功 能: 重定位流上的文件指針
  用 法: int fseek(FILE *stream, long offset, int fromwhere);
  描 述: 函數設置文件指針stream的位置。如果執行成功,stream將指向以fromwhere為基准,偏移offset個字     節的位置。如果執行失敗(比如offset超過文件自身大小),則不改變stream指向的位置。
  返回值: 成功,返回0,否則返回其他值。
  fseek position the file position pointer for the file referenced by stream to the byte location calculated by offset.
  程序例:
  

 1 #include <stdio.h>
 2 long filesize(FILE *stream);
 3 int main(void)
 4 {
 5 FILE *stream;
 6 stream = fopen("MYFILE.TXT", "w+");
 7 fprintf(stream, "This is a test");
 8 printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream));
 9 fclose(stream);
10 return 0;
11 }
12 long filesize(FILE *stream)
13 {
14 long curpos, length;
15 curpos = ftell(stream);
16 fseek(stream, 0L, SEEK_END);
17 length = ftell(stream);
18 fseek(stream, curpos, SEEK_SET);
19 return length;
20 }

 


  int fseek( FILE *stream, long offset, int origin );
  第一個參數stream為文件指針
  第二個參數offset為偏移量,整數表示正向偏移,負數表示負向偏移
  第三個參數origin設定從文件的哪里開始偏移,可能取值為:SEEK_CUR、 SEEK_END 或 SEEK_SET
  SEEK_SET: 文件開頭
  SEEK_CUR: 當前位置
  SEEK_END: 文件結尾
  其中SEEK_SET,SEEK_CUR和SEEK_END和依次為0,1和2.
  簡言之:
  fseek(fp,100L,0);把fp指針移動到離文件開頭100字節處;
  fseek(fp,100L,1);把fp指針移動到離文件當前位置100字節處;
  fseek(fp,100L,2);把fp指針退回到離文件結尾100字節處。
  使用實例:

 1 #include <stdio.h>
 2 #define N 5
 3 typedef struct student {
 4 long sno;
 5 char name[10];
 6 float score[3];
 7 } STU;
 8 void fun(char *filename, STU n)
 9 {
10 FILE *fp;
11 fp = fopen(filename, "rb+");
12 fseek(fp, -1L*sizeof(STU),SEEK_END);
13 fwrite(&n, sizeof(STU), 1, fp);
14 fclose(fp);
15 }
16 void main()
17 {
18 STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
19 {10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},
20 {10005,"ZhangSan", 95, 80, 88}};
21 STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];
22 int i,j; FILE *fp;
23 fp = fopen("student.dat", "wb");
24 fwrite(t, sizeof(STU), N, fp);
25 fclose(fp);
26 fp = fopen("student.dat", "rb");
27 fread(ss, sizeof(STU), N, fp);
28 fclose(fp);
29 printf("\nThe original data :\n\n");
30 for (j=0; j<N; j++)
31 {
32 printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
33 for (i=0; i<3; i++) 
34 printf("%6.2f ", ss[j].score[i]);
35 printf("\n");
36 }
37 fun("student.dat", n);
38 printf("\nThe data after modifing :\n\n");
39 fp = fopen("student.dat", "rb");
40 fread(ss, sizeof(STU), N, fp);
41 fclose(fp);
42 for (j=0; j<N; j++)
43 {
44 printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
45 for (i=0; i<3; i++) 
46 printf("%6.2f ", ss[j].score[i]);
47 printf("\n");
48 }
49 }

 

轉載自:

  https://blog.csdn.net/wl_soft50/article/details/7787521

  https://blog.csdn.net/happy_xiahuixiax/article/details/72900132

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM