gtpc2m3cC/C++ Language Support User's Guide

fsetpos-Set File Position

This function sets a file position.

Format

#include <stdio.h>
int fsetpos(FILE *stream, const fpos_t *pos);

stream
The stream for which the current file position will be set.

pos
The new file position for the stream.

This function moves the file position associated with stream to a new location in the file according to the value of the object pointed to by pos. The value of pos must be obtained by a call to the fgetpos library function. If successful, the fsetpos function clears the end-of file (EOF) indicator and cancels the effect of any previous ungetc function on the same stream.

If the call to the fsetpos function is not valid, the call is treated as a flush and the ungetc characters are discarded.

The fsetpos function handles double-byte character set (DBCS) state information for wide-oriented files. The DBCS shift state is set to the state saved by the fsetpos function. If the record has been updated in the meantime, the shift state may be incorrect.

After the fsetpos call, the next operation on a stream in update mode may be input or output.

Attention: Repositioning in a wide-oriented file and performing updates is not recommended because it is not possible to predict if your update will overwrite part of a multibyte string or character, thereby invalidating subsequent data. For example, you could inadvertently add data that overwrites a shift-out. The following data expects the shift-out to be there, so it is not valid if it is treated as if in the initial shift state. Repositioning to the end of the file and adding new data is safe.

Normal Return

If the fsetpos function successfully changes the current position of the file, it returns a zero value.

Error Return

If there is an error, errno is set and a nonzero value is returned.

Programming Considerations

None.

Examples

The following example opens a file called myfile.dat for reading. After performing input operations (not shown), the file pointer is moved to the beginning of the file and reads the first byte again.

#include <stdio.h>
 
int main(void)
{
 
   FILE *stream;
   int retcode;
   fpos_t pos, pos1, pos2, pos3;
   char ptr[20];  /* existing file 'myfile.dat' has 20 byte records */
 
   /* Open file, get position of file pointer, and read first record */
 
   stream = fopen("myfile.dat", "rb");
   fgetpos(stream,&pos);
   pos1 = pos;
   if (!fread(ptr,sizeof(ptr),1,stream))
       printf("fread error\n");
 
   /* Perform a number of read operations.  The value of 'pos'
      changes if 'pos' is passed to fgetpos()                   */

  ·
  ·
  ·
/* Re-set pointer to start of file and re-read first record */ fsetpos(stream,&pos1); if (!fread(ptr,sizeof(ptr),1,stream)) printf("fread error\n"); fclose(stream); }

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.