A Parser That Refuses Bad Input
MediumWrite the strict integer parser this lesson built, and report its failures where failures belong. main is given to you and does the reading with the chapter 5 loop, fgets into a 128 byte line, the newline stripped with the strlen check, one call to parse_long per line, and a final summary line in the form "parsed M of N lines" that prints on every run including the empty one. What is missing is parse_long(const char *text, long *out), which returns 1 when text is a whole valid number and 0 otherwise, and writes the value through out only when it returns 1. Use the full strtol discipline. Set errno = 0 before the call, because ERANGE is the only channel strtol has for reporting an overflow and a stale value left there by some earlier call would otherwise look like this call's failure. Pass the address of a char *end so strtol can tell you where it stopped, then judge the result: end == text means it converted nothing at all, *end != '\0' means it converted a prefix and stopped at trailing junk, and errno == ERANGE means the digits were fine but the value does not fit in a long. Each of those three is a rejection. The part to get right is where the complaints go. Every rejection message must be written to STANDARD ERROR with fprintf(stderr, ...) or perror(text), never with printf, because this platform compares your standard output against the expected output and a diagnostic printed there would corrupt a run that is otherwise correct. Nothing about the wording of your error messages is checked, so write messages you would actually want to read; only the "parsed N" lines and the summary are compared. Two behaviours of strtol are worth knowing before you start, because they show up in the test cases: it skips leading whitespace, so " 12" is a valid 12, and it accepts a leading sign, so "+5" is 5 and "-17" is -17. An empty line, which is what a blank line becomes after the newline is stripped, has no digits at all and is a rejection like any other. main returns 0 on every path, since the checker treats a nonzero exit status as a failure however right the output looks.
Success Criteria
Your code must pass 8 test case(s) to complete this exercise. 3 hint(s) are available if you need help.
Sign in to track your progress
You can work on exercises as a guest, but sign in to track your progress and save your submissions.