fgetpos

Stores the current file position for a given file.

Format

#include  <stdio.h>

int fgetpos  (FILE *stream, fpos_t *pos);

Arguments

stream
A file pointer.
pos
A pointer to an implementation-defined structure. The fgetpos function fills this structure with information that can be used on subsequent calls to fsetpos.

Description

This function stores the current value of the file position indicator for the stream pointed to by stream into the object pointed to by pos.

Return Values
Indicates successful completion. 
-1  Indicates that there are errors. 

Example

    #include <stdio.h>
    
    main()
    {
    FILE *fp;
    int stat,i;
    int character;
    unsigned char ch, c_ptr[130], d_ptr[130];
    fpos_t posit;
    
    /* Open a file for writing.  */
    
    if ((fp = fopen ("file.dat","w+") ) == NULL)
        {
         perror ("open");
         exit(1);
        }
    
    /* Get the beginning position in the file.  */
    
    if (fgetpos(fp, &posit) != 0)
       perror ("fgetpos");
    
    /* Write some data to the file. */
    
    if (fprintf(fp,"this is a test\n") == 0)
       {
        perror ("fprintf");
        exit(1);
       }
    
    /* Set the file position back to the beginning. */
    
    if(fsetpos(fp, &posit) != 0)
        perror ("fsetpos");
    
    fgets(c_ptr,130,fp);
    puts(c_ptr);  /* Should be "this is a test."  */
    
    /* Close the file. */
    
    if (fclose (fp) != 0)
        {
         perror ("close");
         exit(1);
        }
    
    }
    


Previous Page | Next Page | Table of Contents | Index