Advertisement
Guest User

Untitled

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