Search code examples
ccharstring-comparisonc-stringsstrcmp

How to get lines from two char arrays to check the equality between each of their lines


I'm programming for a uni project. Currently, I have a function for my consumer thread but the issue isn't there now. The issue is that instead of checking the equality of the content(arg->lineF1 and arg->lineF2), I'd like to loop through each of their lines(given that arg->lineF1 and arg->lineF2 are char array) and to check for each of their lines the equality, I've tried some things but they haven't work so far.

I have to specify that the solutions I found to these problems were not working(tried strtok failed, tried looping but I was not getting empty lines).

void * fonctionConsummer(void * args){
    struct argumentThreadConsummer *arg = (struct argumentThreadConsummer *)args;


    //-----------------LINE BY LINE CHECKING------------------------------------
    void * fonctionConsummer(void * args){
    struct argumentThreadConsummer *arg = (struct argumentThreadConsummer *)args;


    //-----------------LINE BY LINE CHECKING------------------------------------
    int counter = 0;
    const char s[2] = "\n";
    char *lineF1;
    char *lineF2;
    lineF1 = strtok(arg->lineF1,s);
    lineF2 = strtok(arg->lineF2,s);
    while(lineF1 != NULL || lineF2 != NULL){
        printf("%d ", counter);
        printf("Line F1 %s\n" , lineF1); // Add the new line character here since it is removed from the lineF1ization process
        printf("Line F2 %s\n" , lineF2); // Add the new line character here since it is removed from the lineF1ization process
        counter++;
        lineF1 = strtok(NULL, s);
        lineF2 = strtok(NULL, s);
    }
    //---------------------------------------------------------------------------

    if(strcmp (arg->lineF1, arg->lineF2) == 0){
        printf("\nLes fichiers ont le meme contenu\n");
        pthread_exit(0);
    }
    printf("\nLes fichiers n'ont pas le meme contenu\n");
    pthread_exit(0);
}

Content of the two files :

content of my two files

the output is :

enter image description here

I don't understand why I'm getting null from F2(in the second iteration)

Thanks in advance for the help, I'm a beginner in C programming.


Solution

  • strtok stores data on the string being parsed in a static location, which means that you can only use it on one string at a time. If you use it on two strings at the same time (like you're trying to do), the second call will clobber the info from the first call. You could use strtok_r instead, but that would still have an additional problem -- blank lines (two consecutive newlines) would be skipped, so your comparisons would (incorrectly?) return a match if the inputs differ by blank lines. You can avoid that problem (as well as the reentrancy) by using strsep instead:

    char *lineF1, *saveF1 = arg->lineF1;
    char *lineF2, *saveF2 = arg->lineF2;
    lineF1 = strsep(&saveF1, s);
    lineF2 = strsep(&saveF2, s);
    while(lineF1 != NULL || lineF2 != NULL){
        :
        lineF1 = strsep(&saveF1, s);
        lineF2 = strsep(&saveF2, s);
    }
    

    if your system doen't have strsep (it's a BSD/GNU libc specific function), you can define it yourself as:

    char *strsep(char **str, const char *delim) {
        char *rv = *str;
        if (rv) {
            *str += strcspn(rv, delim);
            if (**str)
                *(*str)++ = '\0';
            else
                *str = 0; }
        return rv;
    }