Stuck with For loop using fgets for finding duplicate strings that are adjacent to each other

Viewed 36

I'm trying to create a program that reads a text file and that grabs the first line and second line with fgets() storing them into separate arrays for comparison with strcmp() and then running that in a loop to go to the 2nd and 3rd line store it in character array then compare and repeated this process with all the lines until the end of file. I need help in how to do it using a for loop. This is my a.txt file:

Hello?
Hello?
Hello?


No answer? Let's try again:
Hello?
Tik
Tok
Tok
tok

This is what I'm getting back when I compile:

 dup
 L1 Hello?
 L2 Hello?
dup
 L1 Hello?
 L2 Hello?
no dup
 L1 Hello?
 L2 
no dup
 L1 Hello?
 L2 
no dup
 L1 Hello?
 L2 No answer? Let's try again:
dup
 L1 Hello?
 L2 Hello?
no dup
 L1 Hello?
 L2 Tik
no dup
 L1 Hello?
 L2 Tok
no dup
 L1 Hello?
 L2 Tok
no dup
 L1 Hello?
 L2 tok-----
this is the file name:a.txt

Not sure why its repeating Hello's instead of going to the next line Do I have to clear the array somehow? Any help would be awesome, thanks!

--I only inserted relevant code to this post, this isn't all of the code just fyi

char * filename =  argv[argc-1];

FILE *f; 
f = fopen(filename, "r");
if(f == NULL) 
{
      fprintf(stderr, "unable to open file\n");
      return 1;
}

char  line1[MAX_CHAR_FOR_LINE_INPUT];
char  line2[MAX_CHAR_FOR_LINE_INPUT];
    


for (char * j = fgets(line1, MAX_CHAR_FOR_LINE_INPUT,f); j != NULL; j  = fgets(line1, MAX_CHAR_FOR_LINE_INPUT,f) ){
    for (char * n = fgets(line2, MAX_CHAR_FOR_LINE_INPUT,f); n != NULL; n = fgets(line2, MAX_CHAR_FOR_LINE_INPUT,f) ){
        int value = strcmp(line1,line2); 
        if ( value == 0 ){
            printf("dup\n");
        }  
        else{
            printf("no dup\n");
        }
        printf("%s",line1);
        printf("%s",line2);
    }
    printf("-----\n");
}
1 Answers

Try this as a replacement for the nested for loops you've shown:

char line1[ MAX_CHAR_FOR_LINE_INPUT ];
char line2[ MAX_CHAR_FOR_LINE_INPUT ];

fseek( f, 0, SEEK_SET ); // Be sure reading starts at start of file

for( ;; ) { // a "for ever" loop, to be exited somewhere in the code block
    if( fgets( line1, sizeof line1, f ) == NULL ) {
        printf( "At end of file\n" );
        break;
    }
    if( fgets( line2, sizeof line2, f ) == NULL ) {
        printf( "At end of file on unpaired line\n" );
        break;
    }

    if( strcmp( line1, line2 ) == 0 ) // return 0 means "the same"
        puts( "dupe" );
    else
        puts( "no dupe" );

    printf( "%s", line1 );
    printf( "%s", line2 );

    printf("-----\n");
}

It clearly, separately loads first one buffer, then the other, then compares those two (printing stuff), then starts all over again.

EDIT: Asked about the infinite for() loop above sparked a thought...

Here are two more alternatives for the "loop control":

while(  fgets( line1, sizeof line1, f ) != NULL ) { // got one??
    if( fgets( line2, sizeof line2, f ) == NULL ) { // try for another...
        printf( "At end of file on unpaired line\n" );
        break;
    }
    /* ... */
    printf("-----\n");
}

// Or...

while( fgets( line1, sizeof line1, f ) != NULL
    && fgets( line2, sizeof line2, f ) != NULL ) { // try for both

    /* ... */

    printf("-----\n");
}

EDIT FINAL:
The OP has clarified (in comments below) that the objective is to compare EACH line with ALL of its successors.

Since I know I will be paid for this... Here we go:

fseek( f, 0, SEEK_SET ); // Be sure reading starts at start of file

size_t offset = ftell( f );

for( ;; ) { // a "for ever" loop, to be exited somewhere in the code block
    fseek( f, offset, SEEK_SET ); // rewind
    if( fgets( line1, sizeof line1, f ) == NULL ) {
        printf( "At end of file\n" );
        break;
    }
    offset = ftell( f ); // remember this location for next round
    printf( "\n%s", line1 ); // one line in the kitty

    // now, read and compare all subsequent lines...
    while( fgets( line2, sizeof line2, f ) == NULL ) {
        char *eval = "no";
        if( strcmp( line1, line2 ) == 0 ) // return 0 means "the same"
            eval = "dupe >>";

        printf( "%-9s%s", eval, line2 );
    }
    printf("-----\n");
}
Related