title: Is there a way to read a file in C without a bunch of checks for sscanf and fgets?
body: <p>In a program I'm writing, I'm currently doing the part of parsing an input file. I have to do input validation (to some degree), checking if <code>sscanf</code> parses the right number of variables and <code>fgets</code> isn't null. But as a result, the main outline looks like this:</p>
  
  <pre><code>int LINE_LENGTH = 100;
  int parseInput(FILE* fp, FILE* output) {
      char* line = calloc(LINE_LENGTH, sizeof(char));
      if (fgets(line, LINE_LENGTH, fp) == NULL) return 1;
      int camFlag, lightFlag;
      if (sscanf(line, "%d %d %d\n", &amp;frames, &amp;camFlag, &amp;lightFlag) != 3) return 1;
      if (camFlag) {
          if (fgets(line, LINE_LENGTH, fp) == NULL) return 1;
          double cx, cy, cz, dx, dy, dz, dt;
          if (sscanf(line, "%f %f %f %f %f %f %f\n", &amp;cx, &amp;cy, &amp;cz, &amp;dx, &amp;dy, &amp;dz, &amp;dt) != 7) return 1;
          // do stuff with input
      }
      if (lightFlag) {
          if (fgets(line, LINE_LENGTH, fp) == NULL) return 1;
          double cx, cy, cz;
          unsigned char r, g, b;
          if (sscanf(line, "%f %f %f %hhu %hhu %hhu\n", &amp;cx, &amp;cy, &amp;cz, &amp;r, &amp;g, &amp;b) != 6) return 1;
          // do stuff with this data
      }
      for (int i = 0; i &lt; frames; i++) {
          if (fgets(line, LINE_LENGTH, fp) == NULL)) return 1;
          int n;
          if (sscanf(line, "%d\n", &amp;n) != 1) return 1;
          // etc...
      }
  }
  </code></pre>
  
  <p>and as a result, half the lines are checking input. Is there a good way to avoid this?</p>