fseek () function usage

Please indicate the source: https://blog.csdn.net/wl_soft50/article/details/7787521

A little progress every day -> function fseek () usage

In reading the code, I met long ago used fseek (), for a long time to no avail, a little strange, to write out for the next inspection.

Function is a function of the file pointer at the beginning of the file, you need to include the header file stdio.h

fseek
function name: fseek
Function: Relocation stream file pointer on
Usage: int fseek (FILE * stream, long offset, int fromwhere);
Description: The function setting file stream pointer location. If successful, stream fromwhere will point to the reference, the offset position offset bytes. If this fails (such offset exceeds the file size itself), stream pointed position is not changed.
Return Value: success, returns 0, otherwise other values.
fseek position the file position pointer for the file referenced by stream to the byte location calculated by offset.
Example of program:

#include <stdio.h>  
  long filesize(FILE *stream);  
  int main(void)  
  {  
    FILE *stream;  
    stream = fopen("MYFILE.TXT", "w+");  
    fprintf(stream, "This is a test");  
    printf("Filesize of MYFILE.TXT is %ld bytes\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);
The first parameter is the stream file pointer
second parameter offset is the offset represents an integer of a positive offset, a negative number indicates a negative offset to the
origin third parameter set the offset from the beginning of the file where, for the possible values: SEEK_CUR, SEEK_END or SEEK_SET
SEEK_SET: beginning of the file
SEEK_CUR: current location
SEEK_END: the end of the file
where SEEK_SET, SEEK_CUR and SEEK_END and were 0, 1 and 2.
Briefly :
fseek (fp, 100L, 0); fp pointer to the 100 bytes from the beginning of the file;
fseek (fp, 100L,. 1); fp pointer to the 100 byte file from a current position;
fseek (fp, 100L, 2); fp pointer back to the 100 bytes from the end of the file. (Based on the comments of view, it should be fseek (fp, -100L, 2) )
Example:

#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);  
}  
void 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: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);  
   for (i=0; i<3; i++)   
[cpp] view plain copy
 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: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);  
 for (i=0; i<3; i++)   
[cpp] view plain copy
 printf("%6.2f ", ss[j].score[i]);  
 printf("\n");  
}  

Guess you like

Origin blog.csdn.net/u013693952/article/details/93460841