Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your input seems <em>so</em> easy that I would probably just use a loop around <code>fgetc(3)</code>; I'll sketch some pseudo-code here:</p> <pre><code>fd = fopen("file", "r"); int c; while((c=fgetc(fd)) != EOF) { switch(c) { case '0': case '1': /* so on */ case '9': handle_digit(c); break; case ',': handle_comma(); break; case '\n': handle_newline(); break; default: fprintf(stderr, "mistaken input %c\n", c); break; } } fclose(fd); </code></pre> <p>You'll have to manage the input in the functions in a manner that <em>may</em> be a bit awkward if you're used to higher-level languages such as Ruby or Python where you'd just run <code>line.split(',')</code> to get a list of numbers, but that is pretty idiomatic C.</p> <p>Of course, if this were a <em>real</em> problem, I'd probably prefer <code>flex</code> and <code>bison</code>, and write a tiny lexer and grammar, mostly because it would be a <em>lot</em> easier to extend in the future as needs change.</p> <hr> <p><strong>Update</strong></p> <p>With some additional criteria to check, the <code>handle_{digit,comma,newline}()</code> routines are easier to sketch. I'll sketch using global variables, but you could just as easily stuff these into a <code>struct</code> and pass them around from function to function:</p> <pre><code>enum seen { NEWLINE, COMMA, DIGIT, }; enum seen last_seen = NEWLINE; handle_digit(int c) { if (last_seen == DIGIT) { /* error if numbers cannot have multiple digits or construct a larger number if numbers can have multiple digits */ } else if (last_seen == COMMA || last_seen == NEWLINE) { /* start a new entry */ } last_seen = DIGIT; } handle_comma() { if (last_seen == COMMA) { /* error */ } else if (last_seen == NEWLINE) { /* error */ } else if (last_seen == DIGIT) { /* end previous field */ } last_seen = COMMA; } handle_newline() { if (last_seen == NEWLINE) { /* error */ } else if (last_seen == COMMA) { /* error */ } else if (last_seen == DIGIT) { /* end previous field */ } last_seen = NEWLINE; } </code></pre> <p>Add whichever checks you need to validate the contents according to whichever rules you have. You might wish to standardize the order and contents of the tests to ensure that you never forget one, even if it means you write a <code>/* nop */</code> comment once or twice to remind yourself that something is fine.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload