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
- #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 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
- #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", "The 10002," "Caokai" , "A.", ","), ",".
- {10003, "LiSi", "a", ","),{10004,"Fangfang", "a.
- {10005,"Zhangsan", 95, 80, 88}};
- STU n={10006, "Zhaosi",---------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");
- 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 Plaincopy
- printf ("%6.2f", Ss[j].score[i]);
- printf ("\ n");
- }
- Fun ("Student.dat", N);
- printf ("\nthe data after modifing: \ 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 Plaincopy
- printf ("%6.2f", Ss[j].score[i]);
- printf ("\ n");
- }
Transferred from: http://blog.csdn.net/wl_soft50/article/details/7787521
function fseek () usage (RPM)