Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
enum { NO_FILE_POS_VALUES = 3 };

errno_t opener(
  FILE* file, 
  unsigned int *width, 
  unsigned int *height, 
  unsigned int *data_offset
) 
{
  unsigned int file_w;
  unsigned int file_h;
  unsigned int file_o;
  int rc;
  fpos_t offset;

  memset(&offset, 0, sizeof(offset));

  if (file == NULL) { return EINVAL; }
  if (fscanf(file, "%u %u %u", &file_w, &file_h, &file_o)  
      != NO_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;
}

However, because only the return value of a fgetpos() call is a valid argument to be used with 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 file.

...

Code Block
bgColor#CCCCFF
enum { NO_FILE_POS_VALUES = 3 };

errno_t opener(
  FILE* file, 
  unsigned int *width, 
  unsigned int *height, 
  unsigned int *data_offset
) 
{
  unsigned int file_w;
  unsigned int file_h;
  unsigned int file_o;
  int rc;
  fpos_t offset;

  if (file == NULL) { return EINVAL; }
  if ((rc = fgetpos(file, &offset)) != 0 ) { return rc; }
  if (fscanf(file, "%u %u %u", &file_w, &file_h, &file_o)  
      != NO_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;
}

...