Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*=======================================================*/
- /*=======================================================*/
- #include <stdio.h>
- #include <string.h>
- #define MAX_FILENAME_LEN 128
- #define MAX_LINE_LEN 80
- /*=======================================================*/
- void diff(FILE *infile1, FILE *infile2, FILE *outfile);
- /*=======================================================*/
- int main(void)
- {
- /* Declare variables needed for input and output files */
- char filename1[MAX_FILENAME_LEN] = "";
- char filename2[MAX_FILENAME_LEN] = "";
- char outputfile[MAX_FILENAME_LEN] = "";
- FILE *infile1 = NULL;
- FILE *infile2 = NULL;
- FILE *output_file = NULL;
- /* Find out what files should be involved */
- printf("Please enter the name of input file 1\n");
- scanf("%s", filename1);
- printf("%s\n", filename1);
- printf("Please enter the name of input file 2\n");
- scanf("%s", filename2);
- printf("%s\n", filename2);
- printf("Please enter the name of output file \n");
- scanf("%s", outputfile);
- printf("%s\n", outputfile);
- /* Open the specified files */
- infile1 = fopen(filename1, "r");
- infile2 = fopen(filename2, "r");
- output_file = fopen(outputfile, "w");
- /* Change to Put the first line in the output file */
- fprintf(output_file, "File 1 is %s; File 2 is %s\n",
- filename1, filename2);
- /* Do the diff on the files */
- diff(infile1, infile2, output_file);
- /* Close the files */
- fclose(infile1);
- fclose(infile2);
- fclose(output_file);
- return 0;
- }
- /*=======================================================*/
- void diff(FILE *infile1, FILE *infile2, FILE *outfile)
- {
- char file1_line[MAX_LINE_LEN] = "";
- char file2_line[MAX_LINE_LEN] = "";
- int line_number = 0;
- int num_differences = 0;
- int v;
- /* Read a line from each file */
- fgets(file1_line, MAX_LINE_LEN, infile1);
- fgets(file2_line, MAX_LINE_LEN, infile2);
- /* Change this into a loop through the infiles */
- for (line_number=1; line_number<=MAX_LINE_LEN; line_number++)
- {
- v = strcmp (file1_line[line_number], file2_line[line_number]);
- if (v != 0)
- {
- fprintf(outfile, "File 1 (%d):%s",
- line_number, file1_line);
- fprintf(outfile, "File 2 (%d):%s",
- line_number, file2_line);
- num_differences++;
- fgets(file1_line, MAX_LINE_LEN, infile1);
- fgets(file2_line, MAX_LINE_LEN, infile2);
- }
- }
- /* Print the number of differences to the screen */
- printf("Number of differences found: %d\n", num_differences);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement