io - Compare each line from two different files and print the lines that are different in C -


supposing have 2 files this:

file1.txt

john new guy 

file2.txt

man old rick cat dog 

i'd compare first line file1 lines file2 , verify if exist. if not, go 2 second line file1 , compare lines file2.. , on until eof reached file1.

the output expect is:

john new guy 

how thought should done:

  • read file1 , file2
  • create function returns line number of each of them
  • take first line file1 , compare lines file2
  • do until lines file1 wasted

now, don't know i'm doing wrong, don't result expect:

#include <stdio.h> #include <string.h> #include <stdlib.h>  int countlines(char *filename) {                                     int ch = 0, lines = 0;     file *fp = fopen(filename, "r");     if (fp == null)         return 0;      {         ch = fgetc(fp);         if (ch == '\n')             lines++;     } while (ch != eof);      if (ch != '\n' && lines != 0)         lines++;     fclose(fp);      return lines; }  int main(int argc, char *argv[]) {     file *template_file = fopen(argv[1], "r");     file *data_file = fopen(argv[2], "r");      char buffer_line_template_file[100];     char buffer_line_data_file[100];       if (argc != 3)     {         perror("you didn't insert arguments!\n\n");         exit(exit_failure);     }      if (template_file == null || data_file == null)     {         perror("error while opening file!\n\n");         exit(exit_failure);     }      int counter = 0;     (int = 0; < countlines(argv[1]); i++)     {         fgets(buffer_line_template_file, 100, template_file);          (int j = 0; j < countlines(argv[2]); j++)         {             fgets(buffer_line_data_file, 100, data_file);              if (strcmp(buffer_line_template_file, buffer_line_data_file) != 0)             {                 counter++;                 printf("%d", counter);             }         }     }      printf("\n\n");      return 0; } 

could please point me right direction ? testing purposes created counter @ end part of small debug. there should print() function


as per @chux answer got following simplified code:

#include <stdio.h> #include <string.h> #include <stdlib.h>  int main(int argc, char *argv[]) {     file *template_file = fopen(argv[1], "r");     file *data_file = fopen(argv[2], "r");      char buffer_line_template_file[100];     char buffer_line_data_file[100];       if (argc != 3)     {         perror("you didn't insert arguments!\n\n");         exit(exit_failure);     }      if (template_file == null || data_file == null)     {         perror("error while opening file!\n\n");         exit(exit_failure);     }      while(fgets(buffer_line_template_file, 100, template_file))     {         buffer_line_template_file[strcspn(buffer_line_template_file, "\n")] = '\0';          rewind(data_file);         while (fgets(buffer_line_data_file, 100, data_file))         {             buffer_line_data_file[strcspn(buffer_line_data_file, "\n")] = '\0';              if (strcmp(buffer_line_template_file, buffer_line_data_file) != 0)             {                 printf("%s\n", buffer_line_template_file);             }         }     }      printf("\n\n");      return 0; } 

the above code giving me following output, not expected:

john john john john john john is is new new new new new new guy guy guy guy guy guy 

problems op's code

  1. imprecise definition of line.

  2. excessive recalculation

  3. fuzzy determination of number of lines in file.


  1. unlike string, has precise definition in c, reading line not defined. primary specificity issue: line contain trailing '\n'. if first answer yes, last text in file after '\n' constitute line? (excessively long lines issue, let not deal today.)

thus possibly some lines end '\n' , others not, fooling strcmp("dog", "dog\n").

the easiest solution read line until either 1) '\n' encountered, 2) eof occurs or 3) line buffer full. after getting line, lop off potential trailing '\n'.

now lines code subsequently works have no '\n'.

fgets(buffer_line_template_file, 100, template_file); buffer_line_template_file[strcspn(buffer_line_template_file, "\n")] = '\0'; 
  1. op's loop incredible wasteful. consider file 1000 lines. code loop, calling 1000 times countlines() (each countlines() call reads 1000 lines) times when 1 countlines() call suffice.

    // (int j = 0; j < countlines(argv[2]); j++) int j_limit = countlines(argv[2]); (int j = 0; j < j_limit; j++) 
  2. there no need count line anyways, continue until eof (fgets() returns null). no need fix fuzzy definition. (fuzzy-ness concerns same issues #1)

    int counter = 0; (fgets(buffer_line_template_file, 100, template_file)) {   buffer_line_template_file[strcspn(buffer_line_template_file, "\n")] = '\0';    rewind(data_file);   while ((fgets(buffer_line_data_file, 100, data_file)) {     buffer_line_data_file[strcspn(buffer_line_data_file, "\n")] = '\0';      if (strcmp(buffer_line_template_file, buffer_line_data_file) != 0) {       counter++;       printf("%d", counter);     }   } } 

other simplifications possible - day.


fwiw, following counts lines of text allowing last line in file optionally end '\n'.

    unsigned long long filelinecount(file *istream) {       unsigned long long linecount = 0;       rewind(istream);       int previous = '\n';       int ch;        while ((ch = fgetc(inf)) != eof) {          if (previous == '\n') linecount++;         previous = ch;       }       return linecount;     } 

note function may different result fgets() calls. consider file of 1 line of 150 characters. fgets(..., 100,...) report 2 lines. filelinecount() reports 1.

[edit] updated code conform op functionality.

    int found = 0;     while (fgets(buffer_line_data_file, 100, data_file))     {         buffer_line_data_file[strcspn(buffer_line_data_file, "\n")] = '\0';          if (strcmp(buffer_line_template_file, buffer_line_data_file) == 0)         {             found = 1;             break;         }     }     if (!found) printf("%s\n", buffer_line_template_file);