Friday, July 20, 2012

File Positioning Functions in ANSI C

The ANSI library stdio.h provides 5 functions that can be used for file positioning:

int fseek(FILE *stream, long offset, int origin)
Description:
The function sets the current position for stream. A subsequent read or write will access data at the beginning of the new position. The functions takes as parameters a pointer to the accessed file (stream) and two other parameters which will determine the new position (offset and origin). 

The parameter origin represents the reference which will be used for the new position. The parameter may be SEEK_SET (the beginning of the file), SEEK_CUR (current position) or SEEK_END (the end of the file).

The parameter offset represents the offset from origin in bytes. 
Returns:
The function returns a non-zero value when it encountered an error. Otherwise it returns 0.

long ftell(FILE *stream)
Description:
The function returns the current cursor position for stream.
Returns:
The current cursor position in the stream if the operation was successful. Otherwise it returns -1.

void rewind(FILE *stream)
Description:
Puts the file positioning cursor at the start of the file.

int fgetpos(FILE *stream, fpos_t *position)
Description:
The function records in position the current position in stream so it can be used later by int fsetpos(FILE *stream, const fpos_t *ptr).
Returns:
The function returns 0 if it the operation was successful. Otherwise it returns a non-zero value.

int fsetpos(FILE *stream, const fpos_t *position)
Description:
The function positions the stream according to the position. The position is obtained by calling the function int fgetpos(FILE *stream, fpos_t *position).
Returns:
The function returns 0 if it the operation was successful. Otherwise it returns a non-zero value.

Example:
Let us suppose that we have a file "file.txt" with the following content:
123456789
987654321
#include<stdio.h>
/*Legend:
 * The stream cursor will be represented with
 * the letter C.
 */
int main(void)
{
   FILE* f = fopen("file.txt","rt");
   char c;
   long result;
   int i;
   fpos_t position;

   /*Positions the cursor at:
      123C456789\n
      987654321
      The next read character will be 4*/
   fseek(f,3,SEEK_SET);
   c = fgetc(f);
   putchar(c);

   /*Positions the cursor at:
      12C3456789\n
      987654321
      The next read character will be 3*/
   fseek(f,-2,SEEK_CUR);
   c = fgetc(f);
   putchar(c);

   /*Positions the cursor at:
      123456789\n
      98765432C1
      The next read character will be 1*/
   fseek(f,-1,SEEK_END);
   c = fgetc(f);
   putchar(c);

   /*The cursor is now positioned at:
      123456789\n
      987654321C
      The indicated position will be 19 (the end of the file)*/
   result = ftell(f);
   printf("\nCurrent position: %ld",result);

   /*Puts the cursor at the begining of the file*/
   rewind(f);
   /*The cursor is now positioned at:
      C123456789\n
      987654321
      The indicated position will be 19 (the end of the file)*/
   result = ftell(f);
   printf("\nCurren position: %ld\n",result);

   /*Moves the cursor 3 positions (bytes)*/
   for(i = 0; i<3; i++)
   {
      c = fgetc(f);
   }
   /*The cursor is now positioned at:
       123C456789\n
       987654321
   */
   fgetpos(f,&position);
   /*Moves the cursor another 3 positions (bytes)*/
   for(i = 0; i<3; i++)
   {
      c = fgetc(f);
   }
   /*The cursor is now positioned at:
       123456C789\n
       987654321
       The last read character was 6
   */
   putchar(c);
   /*Positions the cursor at the memorized position*/
   fsetpos(f,&position);
   /*The cursor is now positioned at:
        123C456789\n
        987654321
        The next read character will be 4.*/
   c = fgetc(f);
   putchar(c);

   fclose(f);
   return 0;
}

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger