Feb 17, 2022

Comparing two files in C language.

Operations on More than one File. 

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc, char const* argv[])
{
    FILE* file_ptr1;
    FILE* file_ptr2;
    file_ptr1 = fopen("test1.txt", "r");
    file_ptr2 = fopen("test2.txt", "r");

    char word1 [15], word2 [15];
    int total_count = 0;
    while (fscanf(file_ptr1, "%s", word1) != EOF)
    {
        int count = 0;
        fseek(file_ptr2, 0, SEEK_SET);
        while (fscanf(file_ptr2, "%s", word2) != EOF)
        {
            if (strcmp(word1, word2) == 0)
            {
                count++;
            }
        }
        if (count == 0)
        {
            total_count++;
            printf("\"%s\" ---> not in file.\n", word1);
        }
    }
    printf("%d words are not in file.\n", total_count);


    fclose(file_ptr1);
    fclose(file_ptr2);
    return 0;
}

No comments:

Post a Comment

Comparing two files in C language.

Operations on More than one File.    # include < stdio.h > # include < stdlib.h > # include < string.h > int main ( int ...