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.
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()