Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

Dr. A.P.J. Abdul Kalam Technical University, Lucknow

KCS101 / KCS201 Programming for Problem Solving - Using C Language


Lab Exercises


31. Write a program to compare the contents of two files and determine whether they are the same or not.


/*
 File: Prgrm31.c
 Author: Aditya Saini
 Date: Sept 30, 2019
 Description: Program to determine whether two files are the same or not.
*/

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

int main (void)
{
               FILE *f_ptr_1, *f_ptr_2;
               char path [256];
               char string_1 [20];
               char string_2 [20];

               //Input file 1 path.

               printf ("Input first file path: ");
               scanf ("%s", path);

               //Try to open first file

               f_ptr_1 = fopen (path, "r");
               if (f_ptr_1 == NULL)
               {
                              printf ("Error! Unable to open the file.");
                              return -1;
               }



               //Input file 2 path.
               printf ("Input second file path: ");
               scanf ("%s", path);

               //Try to open second file
               f_ptr_2 = fopen (path, "r");
               if (f_ptr_2 == NULL)
               {
                              printf ("Error! Unable to open the file.");
                              fclose (f_ptr_1);
                              return -1;
               }

               //Finding whether the content of files are the same or not.
               while ((! feof (f_ptr_1)) && (! feof (f_ptr_2)))
               {
                              fscanf (f_ptr_1, "%s", string_1);
                              fscanf (f_ptr_2, "%s", string_2);
                              if (strcmp (string_1, string_2) != 0)
                              {
                                             printf ("The content of files are not the same.");
                                             fclose (f_ptr_1);
                                             fclose (f_ptr_2);
                                             return 0;
                              }
               }
               printf ("The content of the files are the same.");
               fclose (f_ptr_1);
               fclose (f_ptr_2);

               return 0;
};

Output

Input first file path: Prgrm31.c
Input second file path: Prgrm31.c
The content of the files are the same.



No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib