Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter algorithm for comparing adjacent lines in a file
    text
    copied!<p>I have completed the assignment (yes it is for a programming class), but I am afraid I didn't go about it in the most efficient way possible. It is basically the uniq program, it will compare adjacent lines in a file and only print one copy of any repeated lines. A few notes: printUniq() is my own function that takes into account various flags, readline() is another function that reads a line of arbitrary length into a char * buffer using malloc and realloc. Here is the part I am worried about:</p> <pre><code>if(prevline != NULL) { while(thisline != NULL) { while(thisline != NULL &amp;&amp; strcmp(prevline, thisline) == 0) { count++; free(prevline); prevline = thisline; thisline = readline(stream); } printUniq(prevline, cflag, dflag, uflag, count); count = 1; free(prevline); if (thisline != NULL) { prevline = thisline; if((thisline = readline(stream)) == NULL) { printUniq(prevline, cflag, dflag, uflag, count); } } } </code></pre> <p>Is there a better way to structure this program? I hate having to check thisline for NULL three times in a loop. The first NULL check in the outer while loop is necessary, and the next check in the nested while is needed in case the last lines are duplicates. The next check after the call to free basically checks if the "Duplicate loop" was exited because of thisline being null, and if not, it will allow the program to get another line. Then the next check is only there for the very last line in the file, because if it weren't there, when readline returns a null (there were no more lines in the file), the loop exits and the prevline was never printed. </p> <p>Anyways, any help is appreciated. </p>
 

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