title: c fscanf error checking
body: <p>I am using fscanf to read from a file in C. I was just wondering if I am correctly checking all the error conditions, and this is the most robust way to do so and I'm not missing anything.</p>
  
  <pre><code>FILE* fp;
  char filename[] = "untitled";
  int count;
  
  char item1[1025];
  char item2[1025];
  
  
  fp = fopen(filename, "r");
  if (fp == NULL) {
      perror("fopen");
      return -1;
  }
  
  count = fscanf(fp, "%1024s%1024s", item1, item2);
  if (count == EOF) {
      if (ferror(fp)) {
          perror("fscanf");
      }
      else {
          fprintf(stderr, "Error: fscanf matching failure\n");
      }
      return -1;
  }
  else if (count == 0) {
      fprintf(stderr, "Error: fscanf early matching failure\n");
      return -1;
  }
  else if (count != 2) {
      fprintf(stderr, "Error: fscanf matched less items than expected\n");
      return -1;
  }
  
  if (fclose(fp) == EOF) {
      perror("fclose");
      return -1;
  }
  </code></pre>
  
  <p>Thank you for your time.</p>
  
  <p>EDIT after comments:</p>
  
  <pre><code>count = fscanf(fp, "%1024s%1024s", item1, item2);
  if (count == EOF) {
      if (ferror(fp)) {
          perror("fscanf");
      }
      else {
          fprintf(stderr, "Error: fscanf reached end of file, no matching characters, no matching failure\n");
      }
      return -1;
  }
  else if (count != 2) {
      fprintf(stderr, "Error: fscanf successfully matched and assigned %i input items, 2 expected\n", count);
      return -1;
  }
  </code></pre>