複製鏈接
請複製以下鏈接發送給好友

fseek

鎖定
int fseek(FILE *stream, long offset, int fromwhere);函數設置文件指針stream的位置。
fseek函數和lseek函數類似,但lseek返回的是一個off_t數值,而fseek返回的是一個整型
外文名
fseek
類    型
函數
領    域
計算機
注    意
不是定位文件指針

fseek功 能

重定位流(數據流/文件)上的文件內部位置指針
注意:文件指針指向文件/流。位置指針指向文件內部的字節位置,隨着文件的讀取會移動,文件指針如果不重新賦值將不會改變或指向別的文件。

fseek頭文件

#include <stdio.h>

fseek用 法

int fseek(FILE *stream, long offset, int fromwhere);

fseek描 述

如果執行成功,stream將指向以fromwhere(偏移起始位置:文件頭0(SEEK_SET),當前位置1(SEEK_CUR),文件尾2(SEEK_END))為基準,偏移offset(指針偏移量)個字節的位置。如果執行失敗(比如offset超過文件自身大小),則不改變stream指向的位置。

fseek返回值

成功,返回0,失敗返回非0值,並設置error的值,可以用perror()函數輸出錯誤。
fseek position the file(文件) position(位置) pointer(指針) for the file referenced by stream to the byte location calculated by offset.

fseek程序例

#include <stdio.h>
long filesize(FILE*stream);
int main(void)
{
	FILE *stream;
	stream=fopen("MYFILE.TXT","wb+");
	fprintf(stream,"Thisisatest");
	printf("FilesizeofMYFILE.TXTis%ldbytes\n",filesize(stream));
	fclose(stream);
	return 0;
}

long filesize(FILE*stream)
{
	long curpos,length;
	curpos=ftell(stream);
	fseek(stream,0L,SEEK_END);
	length=ftell(stream);
	fseek(stream,curpos,SEEK_SET);
	return length;
}
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);把stream指針移動到離文件開頭100字節處;
fseek(fp,100L,1);把stream指針移動到離文件當前位置100字節處;
fseek(fp,-100L,2);把stream指針退回到離文件結尾100字節處。
使用實例:
#include<stdio.h>
#define N 5
typedef struct student{
  long sno;
  char name[10];
  float score[3];
}STU;
 
void fun(char*filename,STU n)
{
  FILE*fp;
  fp=fopen(filename,"rb+");
  fseek(fp,-1L*sizeof(STU),SEEK_END);
  fwrite(&n,sizeof(STU),1,fp);
  fclose(fp);
}
 
int main()/*修改覆蓋最後一個學生數據*/
{
  STU t[N]={
    {10001,"MaChao",91,92,77},
    {10002,"CaoKai",75,60,88},
    {10003,"LiSi",85,70,78},
    {10004,"FangFang",90,82,87},
    {10005,"ZhangSan",95,80,88}
  };
  
  STU n={10006,"ZhaoSi",55,70,68},ss[N];

  int i,j;FILE*fp;
  
  fp=fopen("student.dat","wb");
  
  fwrite(t,sizeof(STU),N,fp);
  
  fclose(fp);
  
  fp=fopen("student.dat","rb");
  
  fread(ss,sizeof(STU),N,fp);
  
  fclose(fp);
  
  printf("\nThe original data:\n\n");
  
  for(j=0;j<N;j++)
  {
    printf("\nNo:%ldName:%-8sScores:",ss[j].sno,ss[j].name);
    for(i=0;i<3;i++)
        printf("%6.2f",ss[j].score[i]);
    printf("\n");
  }
  fun("student.dat",n);
  printf("\nThe data after modifing:\n\n");

  fp=fopen("student.dat","rb");
  fread(ss,sizeof(STU),N,fp);
  fclose(fp);

  for(j=0;j<N;j++)
  {
    printf("\nNo:%ldName:%-8sScores:",ss[j].sno,ss[j].name);
    for(i=0;i<3;i++)
        printf("%6.2f",ss[j].score[i]);
    printf("\n");
  }
  return 0;
}

fseek注意事項

fseek函數的文件指針,應該為已經打開的文件。如果沒有打開的文件,那麼將會出現錯誤。 fseek函數也可以這樣理解,相當於在文件當中定位。這樣在讀取規律性存儲文件時可以利用其OFFSET偏移量讀取文件上任意的內容。
fseek函數一般用於二進制文件,也可以用於文本文件。用於文本文件操作時,需特別注意只有fseek(fp, 0, SEEK_SET) 和 fseek(fp, ftell(fp), SEEK_SET)能確保結果符合預期。