Advertisement
Guest User

Untitled

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