function fseek () usage

Source: Internet
Author: User

In reading the code, encountered a very early use of the fseek (), a long time no use, a little strange, write for the next review.

function function is to point the file pointer to the beginning of the file, need to include the header file stdio.h

fseek function Name: fseek function: Reposition the file pointer on the stream usage: int fseek (file *stream, long offset, int fromwhere); Description: The function sets the location of the file pointer stream. If the execution succeeds, the stream points to a position that offsets the offset byte as a datum of fromwhere.   If execution fails (for example, offset exceeds the size of the file itself), the location pointed to by the stream is not changed.   Return value: Success, returns 0, otherwise returns other values.   Fseek position the file position pointer for the file referenced by stream to the byte location calculated by offset. program Example:

[CPP]View Plaincopy
  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); The first parameter stream is the file pointer, the second parameter offset is offset, the integer represents a forward offset, and a negative number indicates a negative offset. The third parameter, origin setting, is offset from where the file begins, possibly with a value of: Seek_cur, Seek_end, or Seek_set seek_   SET: File start Seek_cur: Current position seek_end: End of file where Seek_set,seek_cur and Seek_end and sequentially 0, 1, and 2. In short: fseek (fp,100l,0); Move the fp pointer to 100 bytes from the beginning of the file, Fseek (fp,100l,1), move the fp pointer to 100 bytes from the current position of the file; fseek (fp,100l,2);   Returns the fp pointer to 100 bytes from the end of the file. Usage examples:

[CPP]View Plaincopy
  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", "The 10002," "Caokai" , "A.", ","), ",".
  19. {10003, "LiSi", "a", ","),{10004,"Fangfang", "a.
  20. {10005,"Zhangsan", 95, 80, 88}};
  21. STU n={10006, "Zhaosi",---------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");
  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++)
[CPP]View Plaincopy
  1. printf ("%6.2f", Ss[j].score[i]);
  2. printf ("\ n");
  3. }
  4. Fun ("Student.dat", N);
  5. printf ("\nthe data after modifing: \ n");
  6. fp = fopen ("Student.dat", "RB");
  7. Fread (SS, sizeof (STU), N, FP);
  8. Fclose (FP);
  9. for (j=0; j<n; j + +)
  10. {
  11. printf ("\nno:%ld Name:%-8s Scores:", Ss[j].sno, Ss[j].name);
  12. For (i=0; i<3; i++)
[CPP]View Plaincopy
    1. printf ("%6.2f", Ss[j].score[i]);
    2. printf ("\ n");
    3. }

Transferred from: http://blog.csdn.net/wl_soft50/article/details/7787521

function fseek () usage (RPM)

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.