Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using <code>(thisLine = readline()) != NULL</code> as the condition of the loop, and only read <strong>one</strong> line at a time, would mean that the loop stopped at the end of the file, and the body of the loop could only be entered when thisLine is valid.</p> <p>It could either read prevLine outside the loop, or deal with no previous line inside the loop:</p> <pre><code>if ((thisLine = readline()) != NULL) { char* prevLine = thisLine; // got one line while ((thisLine = readline()) != NULL) { if (strcmp(...) == 0) { ... } else { ... } ... } ... deal with prev_line, no need for if because it *must* have been read. } </code></pre> <p>vs</p> <pre><code>prevLine = NULL; while ((thisLine = readline()) != NULL) { if (prevLine == NULL) { // first line? ... } else if (strcmp(...) == 0) { ... } else { ... } ... prev_line = this_line } if (prev_line != NULL) { ... } else { // only one line in the file? ... } </code></pre> <p>Dealing with the one line outside the loop makes the the first approach simpler. The flow seems very clear to me. Is there one line? Is there a second line? Okay, uniqueness has meaning ... </p> <p>In the second approach, dealing with one line inside the loop means the maintenance developer will be looking at the first-line test for every line of input, which IMHO is more for them to worry about. </p> <p>Code after the loop is necessary because the last two or more lines of the file might be repeated, and that case needs to call printUnique too.</p> <p>Also one-line-file logic is after the loop (which is less clear, IMHO) and would require mre logic. This logic is needed if the program is intended to emulate other <code>uniq</code> functionality like printing <em>every</em> line with a count. </p> <p>The clear benefit of the second approach is reading the file in one place, which is in general a good tactic. IMHO, if readline is written properly, it doesn't matter much. </p> <p>Summary: the first approach needs less logic, and the order of events is more explicit, hence it is simpler to understand. The second reads the file in one place, but needs the last repeating group to be handled outside the loop, so it is even longer. It is also more logic if <code>uniq</code> in general is being programmed.</p> <p>Note: Both of these flows work. </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. This table or related slice is empty.
    1. 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