Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. /*=======================================================*/
  2. /*Name: John Jordan8*/
  3. /* Section 1*/
  4. /*=======================================================*/
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. #define MAX_FILENAME_LEN 128
  9. #define MAX_LINE_LEN 80
  10.  
  11. /*=======================================================*/
  12. void diff(FILE *infile1, FILE *infile2, FILE *outfile);
  13.  
  14. /*=======================================================*/
  15. int main(void)
  16. {
  17. /* Declare variables needed for input and output files */
  18. char filename1[MAX_FILENAME_LEN] = "";
  19. char filename2[MAX_FILENAME_LEN] = "";
  20. char outputfile[MAX_FILENAME_LEN] = "";
  21. FILE *infile1 = NULL;
  22. FILE *infile2 = NULL;
  23. FILE *output_file = NULL;
  24.  
  25. /* Find out what files should be involved */
  26. printf("Please enter the name of input file 1\n");
  27. scanf("%s", filename1);
  28. printf("%s\n", filename1);
  29. printf("Please enter the name of input file 2\n");
  30. scanf("%s", filename2);
  31. printf("%s\n", filename2);
  32. printf("Please enter the name of output file \n");
  33. scanf("%s", outputfile);
  34. printf("%s\n", outputfile);
  35.  
  36.  
  37. /* Open the specified files */
  38. infile1 = fopen(filename1, "r");
  39. infile2 = fopen(filename2, "r");
  40. output_file = fopen(outputfile, "w");
  41.  
  42.  
  43.  
  44. /* Change to Put the first line in the output file */
  45. fprintf(output_file, "File 1 is %s; File 2 is %s\n",
  46. filename1, filename2);
  47.  
  48. /* Do the diff on the files */
  49. diff(infile1, infile2, output_file);
  50.  
  51. /* Close the files */
  52. fclose(infile1);
  53. fclose(infile2);
  54. fclose(output_file);
  55.  
  56. return 0;
  57. }
  58.  
  59. /*=======================================================*/
  60. void diff(FILE *infile1, FILE *infile2, FILE *outfile)
  61. {
  62. char file1_line[MAX_LINE_LEN] = "";
  63. char file2_line[MAX_LINE_LEN] = "";
  64. int line_number = 0;
  65. int num_differences = 0;
  66. int v;
  67.  
  68. /* Read a line from each file */
  69. fgets(file1_line, MAX_LINE_LEN, infile1);
  70. fgets(file2_line, MAX_LINE_LEN, infile2);
  71.  
  72. /* Change this into a loop through the infiles */
  73. for (line_number=1; line_number<=MAX_LINE_LEN; line_number++)
  74. {
  75.  
  76. v = strcmp (file1_line, file2_line);
  77. if (v != 0)
  78. {
  79. fprintf(outfile, "File 1 (%d):%s",
  80. line_number, file1_line);
  81. fprintf(outfile, "File 2 (%d):%s",
  82. line_number, file2_line);
  83. num_differences++;
  84.  
  85. fgets(file1_line, MAX_LINE_LEN, infile1);
  86. fgets(file2_line, MAX_LINE_LEN, infile2);
  87. }
  88. }
  89.  
  90. /* Print the number of differences to the screen */
  91. printf("Number of differences found: %d\n", num_differences);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement