 
                            ...
Consequently, using any other values for pos results in undefined behavior and should be avoided.
Noncompliant Code Example
...
| Code Block | ||||
|---|---|---|---|---|
| 
 | ||||
| #include <stdio.h>
#include <string.h>
 
enum { NO_FILE_POS_VALUES = 3 };
int opener(FILE *file) {
  int rc;
  fpos_t offset;
  memset(&offset, 0, sizeof(offset));
  if (file == NULL) { return -1; }
  /* Read in data from file. */
  rc = fsetpos(file, &offset);
  if (rc != 0 ) { return rc; }
  return 0;
}
 | 
However, because only the return value of an fgetpos() call is a valid argument to fsetpos(), passing an fpos_t value that was created in any other way may not work. It is possible that the position will be set to an arbitrary location in the fileis undefined behavior.
Compliant Solution
In this compliant solution, the initial file position indicator is stored by first calling fgetpos(), which is used to restore the state to the beginning of the file in the later call to fsetpos():
| Code Block | ||||
|---|---|---|---|---|
| 
 | ||||
| #include <stdio.h>
#include <string.h>
 
enum { NO_FILE_POS_VALUES = 3 };
int opener(FILE *file) {
  int rc;
  fpos_t offset;
  if (file == NULL) { return -1; }
  rc = fgetpos(file, &offset);
  if (rc != 0 ) { return rc; }
  /* Read in data from file. */
  rc = fsetpos(file, &offset);
  if (rc != 0 ) { return rc; }
  return 0;
}
 | 
...