Make a little progress every day -- & gt; how to use the function fseek ()

Source: Internet
Author: User

When I was reading the code, I encountered the fseek () that I used very early. It was not practical for a long time. It was a little strange. I wrote it for future reference.

The function is to point the file pointer to the beginning of the file and must include the header file stdio. h.

Fseek
 Function Name: fseek
 Function: relocate the file pointer on the stream
 Usage: int fseek (File * stream, long offset, int fromwhere );
 Description: The function sets the position of the file pointer stream. If the operation is successful, stream points to the position of the Offset section based on fromwhere. If the operation fails (for example, the offset value exceeds the file size), the position pointed to by the stream is not changed.
 Return Value: success. 0 is returned. Otherwise, other values are returned.
 Fseek position the file position pointer for the file referenced by stream to the byte location calculated by offset.
 Program example:
  

#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 batch number stream is the file pointer.
 The second offsets offset is the offset. An integer indicates the positive offset and a negative value indicates the negative offset.
 The third sequence number origin sets the start offset of the object. The possible values are seek_cur, seek_end, and seek_set.
 Seek_set: Start of the file
 Seek_cur: current location
 Seek_end: End of the file
 Seek_set, seek_cur, and seek_end are respectively 0, 1 and 2.
 In short:
 Fseek (FP, 100l, 0); move the FP pointer to the first 100 bytes from the file;
 Fseek (FP, 100l, 1); move the FP pointer to 100 bytes away from the current position of the file;
 Fseek (FP, 100l, 2); returns the FP pointer to 100 bytes away from the end of the file.
 Instance used:

  #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++) 
     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++) 
     printf("%6.2f ", ss[j].score[i]);     printf("\n");    }  }



 

Make a little progress every day -- & gt; how to use the function fseek ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.