Write a C program that simulates a different version of Unix

Write a C program that simulates a different version of Unix’s diff command (to be called diffnew.c). For example, given two text files file1.txt and file2.txt: (Assume max line size in either file is 256)

diffnew      file1.txt    file2.txt flag

returns:

o   Zero (0) if the two files are exactly identical

o   -1 if either one of the two files is missing or cannot be opened, or an illegal flag value is used. Provide an explanation of the encountered error.

o   A positive integer indicating:

§ the number of characters that are different in the two files (if 1 flag is used)

§ the number of words that are different in the two files (if 2 flag is used)

§ the number of lines that are different in the two files (if 3 flag is used)

Example output would look like

           Files contain 15 different characters.

Solution

Hi,

According to your requiement I\'m writing C code. Please find below code for different diffferent functionalities.

1) Zero (0) if the two files are exactly identical

Hi,

According to your requiement I\'m writing C code. Please find below code for different diffferent functionalities.

1) Zero (0) if the two files are exactly identical

2)  -1 if either one of the two files is missing or cannot be opened, or an illegal flag value is used. Provide an explanation of the encountered error.

3)

A positive integer indicating:

§ the number of characters that are different in the two files (if 1 flag is used)

§ the number of words that are different in the two files (if 2 flag is used)

§ the number of lines that are different in the two files (if 3 flag is used)

According to your requirement I have written the code but i haven\'t got any chanace to test it If your facing any issue please let me know.

Program

*******************************************************************************************************

#include<stdio.h>

int main() {

FILE *fp1, *fp2, *fp3;

int ch1, ch2, linecount, wordcount, charcount;;

char file1[40], file2[40];

   // Initialize counter variables
   linecount = 0;
   wordcount = 0;
   charcount = 0;

printf(\"Enter name of first file :\");

gets(file1);

printf(\"Enter name of second file:\");

gets(file2);

fp1 = fopen(file1, \"r\");

fp2 = fopen(file2, \"r\");

if (fp1 == NULL) {

printf(\"Cannot open %s for reading \", file1);

exit(-1);

} else if (fp2 == NULL) {

printf(\"Cannot open %s for reading : \", file2);

exit(-1);

} else {

ch1 = getc(fp1);

ch2 = getc(fp2);

while ((ch1 != EOF) && (ch2 != EOF) && (ch1 == ch2)) {

ch1 = getc(fp1);

ch2 = getc(fp2);

}

   //For file lines,character count
   if (ch1 != ch2){
   while ((ch1=getc(fp)) != EOF || (ch2=getc(fp)) != EOF ) {
      
      // Increment character count if NOT new line or space
       if (ch1 != \' \' && ch2!= \' \' && ch1 != \'\ \' && ch2 != \'\ \') { ++charcount; }
      
       // Increment word count if new line or space character
       if (ch1 != \' \' && ch2!= \' \' || ch1 != \'\ \' && ch2 != \'\ \') { ++wordcount; }
         
       // Increment line count if new line character
       if (ch1 == \'\ \' && ch2 != \'\ \') { ++linecount; }
   }

   if (charcount > 0) {
       ++linecount;
       ++wordcount;
   }
   }

if (ch1 == ch2)

printf(\"Files are identical n\");

else if (ch1 != ch2)

printf(\"Files are Not identical n\");

fclose(fp1);

fclose(fp2);

}

return (0);

}

Write a C program that simulates a different version of Unix’s diff command (to be called diffnew.c). For example, given two text files file1.txt and file2.txt:
Write a C program that simulates a different version of Unix’s diff command (to be called diffnew.c). For example, given two text files file1.txt and file2.txt:
Write a C program that simulates a different version of Unix’s diff command (to be called diffnew.c). For example, given two text files file1.txt and file2.txt:

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site