0% found this document useful (0 votes)
2K views5 pages

Program 31 Objective: Write A Program Tocompare The Content of Two Files and Whether They Are Same or Not

The document describes a C program to compare the contents of two files and check if they are the same or not. It opens both files, reads them line by line, and compares the contents using string comparison functions. If any line is different, it prints that the files are not the same, otherwise it prints that the files are the same.

Uploaded by

ayushivats509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views5 pages

Program 31 Objective: Write A Program Tocompare The Content of Two Files and Whether They Are Same or Not

The document describes a C program to compare the contents of two files and check if they are the same or not. It opens both files, reads them line by line, and compares the contents using string comparison functions. If any line is different, it prints that the files are not the same, otherwise it prints that the files are the same.

Uploaded by

ayushivats509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM 31

OBJECTIVE: WRITE A PROGRAM TOCOMPARE THE CONTENT OF TWO FILES AND WHETHER THEY ARE
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 fi//Input file 2 path.");

printf ("Input second file path: ");

scanf ("%s", path);

//Try to open second file

AYUSHI VATS B4 G2 22AIML103


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);

le.");

AYUSHI VATS B4 G2 22AIML103


return 1;

OUTPUT:

Input first file path: Prgrm31.c


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

AYUSHI VATS B4 G2 22AIML103


PROGRAM 32

OBJECTIVE: WRITE A PROGRAM TO TO CHECK WHETHER A GIVEN WORD EXISTSING AFILE OR NOT.IF
YES THEN FIND THE NUMBER OF TIMES IT OCCURS.

#include<stdio.h>
void main()
{

FILE* filePointer;

int wordExist=0;

int bufferLength = 255;

char search[100];

printf("Enter word to be search=");

scanf("%s",search);

char line[bufferLength];

filePointer = fopen("D:\\file.txt", "r");

while(fgets(line, bufferLength, filePointer))

char *ptr = strstr(line, search);

if (ptr != NULL)

wordExist=1;

break;

fclose(filePointer);

if (wordExist==1)

AYUSHI VATS B4 G2 22AIML103


printf("Word exists.");

else

printf("Word doesn't exist.");

OUTPUT:

Enter word to be search=is

Word exists.

AYUSHI VATS B4 G2 22AIML103

You might also like