1

Here's my issue I'm running into.

I'm reading in lines of text from a file, but only trying to read in lines that aren't consecutive repeats.

Here's my regular code for reading in the lines of text.

  while (((ch = getc (fp)) != EOF))
         {
            a[j++] = ch;
         }   
          a[j] = '\0';

which works just fine.

When trying to figure out how to go about the problem, I tried having a char array that would read in a line at a time, and then compare it to the previous line using strcomp. If it was a match, it would not add that line into the final char array. It looks something like this:

  while (((ch = getc (fp)) != EOF))
        {
            if (ch != '\n')
            {
            copynumber++;
            temp[j] = ch;
            }
            else 
            {
            uni = strcmp(identical, final);
                if (uni == 0) {
                    copynumber = 0;
                }
                else 
                {
                    strncpy(identical, temp, copynumber);
                    final[j] = ch;
                }
                j++;
            }

        }
          final[j] = '\0';

but I know this won't work for a few reasons. One, I never add the previous chars into the final array. I'm really just lost. Any help is appreciated. Thank you.

jtuntalan
  • 21
  • 4

1 Answers1

0

There is a getline() function in stdio.h that you can use to get each line in a file.

To skip duplicates you can store the previous line read and compare at each step.

FILE *fp;
char *line = NULL;
char *prev_line[999];
char final[999];
size_t len = 0;
ssize_t read;

fp = fopen("file", "r");

while ((read = getline(&line, &len, fp)) != -1) { // Read each line
    if (strncmp(line, prev_line, read) != 0) {    // If not equal
        strncat(final, line, read);                // Copy to final
        strncpy(prev_line, line, read);           // Update previous
    }
}

free(line); // Need to free line since it was null when passed to getline()
Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
  • hmm. getting an error that there are too many arguments for strcat – jtuntalan Apr 30 '18 at 04:24
  • *`strncat`. There should have been an n in there – Increasingly Idiotic Apr 30 '18 at 04:25
  • Also, what is the difference between using ssize_t read vs size_t read in this context? Initially, ssize caused an error, but everything worked fine when I changed it to size – jtuntalan Apr 30 '18 at 04:31
  • I see. Last thing: I incorporated this code into something where I'm using command line arguments to read in from multiple files. Everything works fine with only one file to open, but not when there's multiple. I get a double free error, since I assume free(line) is invoked twice (I don't know much about how free works). Any solution you can thing of? – jtuntalan Apr 30 '18 at 04:40
  • I'm not really sure. I'd need to see an entire example. If you cannot figure it out by googling I'd recommend posting a new question with more details when you are able to. You might try moving the `free(line)` to the very end of the function if you think that is the issue though. – Increasingly Idiotic Apr 30 '18 at 04:48
  • [Here's the code if you wanted to take a look. If not, I'll try posting a new question.](https://onlinegdb.com/SJyMN7Vpz) – jtuntalan Apr 30 '18 at 05:06