 
                            ...
The following non-compliant code attempts to read three values from a file, then set the cursor position back to the beginning of the file and return to the caller.
| Code Block | ||
|---|---|---|
| 
 | ||
| enum { NO_FILE_POS_VALUES = 3 }; errno_t opener(FILE* file, int *width, int *height, int *data_offset) { int file_w; int file_h; int file_o; int rc; int offset = 0; if (file == NULL) { return EINVAL; } if (fscanf(file, "%i %i %i", &file_w, &file_h, &file_o) != 3NO_FILE_POS_VALUES) { return EIO; } if ((rc = fsetpos(file, &offset)) != 0 ) { return rc; } *width = file_w; *height = file_h; *data_offset = file_o; return 0; } int main(void) { int width; int height; int data_offset; FILE *file; ... file = fopen("myfile", "rb"); if(opener(file, &width, &height, &data_offset) != 0 ) { return 0; } ... } | 
...
In this compliant solution, the initial file position indicator is stored by first calling fgetpos(), which is used to restore the state back to the beginning of the file in the later call to fsetpos().
| Code Block | ||
|---|---|---|
| 
 | ||
| enum { NO_FILE_POS_VALUES = 3 }; errno_t opener(FILE* file, int *width, int *height, int *data_offset) { int file_w; int file_h; int file_o; int rc; fpos_t offset; if (file == NULL) { return EINVAL; } if ((rc = fgetpos(file, &offset)) != 0 ) { return rc; } if (fscanf(file, "%i %i %i", &file_w, &file_h, &file_o) != 3NO_FILE_POS_VALUES) { return EIO; } if ((rc = fsetpos(file, &offset)) != 0 ) { return rc; } *width = file_w; *height = file_h; *data_offset = file_o; return 0; } int main(void) { int width; int height; int data_offset; FILE *file; ... file = fopen("myfile", "rb"); if (opener(file, &width, &height, &data_offset) != 0 ) { return 0; } ... } | 
...