> fseek<stdio.h> ~ Online tutorial

fseek



fseek          <STDIO.H>

 Repositions the file pointer of a stream

 Declaration:

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

 Remarks:


fseek sets the file pointer associated with a stream to a new position.

fseek discards any character pushed back using ungetc.

fseek is used with stream I/O. For file handle I/O, use lseek.

After fseek, the next operation on an update file can be either input or
output.

fseek can return a 0 (indicating that the pointer has been moved
successfully), when it has not been. This is because DOS, which actually
resets the pointer, does not verify the setting.

 Return Value:


   On success (the pointer is successfully moved), fseek returns 0.
   On failure, fseek returns a non-zero value. fseek returns an error code
    only on an unopened file or device.


 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;
 }

Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: