0

I need to compare two files and create a new one with the differences of this two, i just know compare the files to show if they are equal or not, but i dont know how can i solve my problem.

I search so much info on how to compare files, but i didnt found the way of display the differences between this two files. This is my actual code.

Edit: The file can contain more than 1 line of data, usually like 30.

The text cannot be deleted, and the similarity just appear in the end of the text. I mean, the final line of a file text will be always the start of the other file text.

This is for homework and the instruction are: "Read the host file and check if any line is already present in the ip url file, and in case they are not, it is necessary to write them in a temporary file."

#include <stdio.h>
void main() 
{
FILE *f1, *f2;
int a, b;

f1 = fopen("D:\\product3\\test.txt","r");
f2 = fopen("D:\\product3\\test2.txt", "r");

if(f1 == NULL || f2 == NULL)
{
    printf("Cannot read the files");
}
else
{
    a = getc(f1);
    b = getc(f2);

    while (a != EOF && b != EOF) 
    {
        a = getc(f1);
        b = getc(f2);

        if(a != b)
        {
            printf("");
        }
    }
    if (a == b) 
    {
        printf("\n The files are equal \n");
    }
    else if (a!=b) 
    {
        printf("\n The files aren't equal \n");
    }
    else 
    {
        printf("\n Error \n");
    }
}

fclose(f1);
fclose(f2);
}

The content of the files are:

>Test.txt
AAA
aaa
bbb
BBB
111

>Test2.txt
AAA
aaa
bbb
BBB
111
333
444
555
6666
777
CCC

so the output must be:

>333
444
555
6666
777
CCC
  • Welcome so SO. Is your file containing only 1 line of data? Is it expected to have less data in Test.txt? – Raka May 31 '19 at 05:13
  • No, can contain more than 1 line of data, like 30 usually – Alberto fernandez moreno May 31 '19 at 05:19
  • I think you are probably looking something like [link](https://stackoverflow.com/questions/14370070/compare-two-text-files-line-by-line/14370155) – Raka May 31 '19 at 05:25
  • This link is in C# and i work with C. Do you know a similar post in C? – Alberto fernandez moreno May 31 '19 at 05:28
  • 1
    Your example doesn't really explain what you want. In the example, `Test2.txt` is `Test.txt` with extra contents at the end. Is that the only thing you need to detect? Can the new text appear in the middle? Can text be deleted? Generalized file comparison is a [longest common subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem) problem. – Raymond Chen May 31 '19 at 05:35
  • 1
    I assume this is homework. If it is, please copy the exact wording of the instructions. – klutt May 31 '19 at 05:37
  • Sorry, the text cannot be deleted, and the similarity just appear in the end of the text. I mean, the final line of a file text will be always the start of the other file text – Alberto fernandez moreno May 31 '19 at 05:39
  • 1
    @Broman Yes, this is for homework, this is the instruction: Read the host file and check if any line is already present in the ip url file, and in case they are not, it is necessary to write them in a temporary file. – Alberto fernandez moreno May 31 '19 at 05:43
  • 1
    Don't add it in the comments. Edit the question. – klutt May 31 '19 at 05:48
  • Check this link to convert c# code to C [C#ToC_Converter](https://stackoverflow.com/questions/8385606/are-there-any-c-sharp-to-c-converter-tools) – Raka May 31 '19 at 05:49
  • `printf("");` does nothing. – Keith Thompson May 31 '19 at 06:00
  • Why not take a line-oriented approach using `fgets` and a pair of buffers? That way in a loop you could read a line at a time from each file and compare them, outputting the 2nd buffer only if it differs from the first and breaking the loop when `fgets` returns `NULL` on either read. Following this loop, just check which files is at `EOF` with, e.g. `feof(f1)` and output all lines from the other file until it also reaches `EOF`? (**note:** you should trim the `'\n'` from each buffer before comparison to protect against a missing line-ending on the final line of either file) – David C. Rankin May 31 '19 at 07:00

3 Answers3

1

Let's add a little more code into the while loop body to show the difference:

while (a != EOF && b != EOF) 
{
    a = getc(f1);
    b = getc(f2);

    if(a != b)
    {
        /* print info about this difference */
    }
}

And after this let's add an information about difference of of file length:

if (a == EOF || b == EOF) 
{
    /* print other characters of the longer file */
}

Please add comments here if you see any problems in these small steps.

And the last step will be simple: save the difference in file (but before compliting of both first steps I'd recommend you to use printf for printing of everything).

Ilya
  • 4,583
  • 4
  • 26
  • 51
  • This can work when the files start with the same line, but is not always like this. I just need to find equal lines, i dont need equal character. – Alberto fernandez moreno May 31 '19 at 05:30
  • Ok, I see, you extended the question. But the difference is not so big: you can read strings line-by-line (now you are reading characters one-by-one), and compare them. If i-th lines in both files are equal, you read next lines. If there is a difference, you print a message about this. – Ilya May 31 '19 at 05:44
  • But how i know the content of the line how is equal or not for save in a new file? – Alberto fernandez moreno May 31 '19 at 05:47
  • @Albertofernandezmoreno There is a function `strcmp` for comparation of strings. See here - [http://www.cplusplus.com/reference/cstring/strcmp/](http://www.cplusplus.com/reference/cstring/strcmp/) – Ilya May 31 '19 at 06:19
1

Well, your algorithm is wrong. Here is some pseudo code that might help:

function line_exists(l, la)
    for line in la
        if line == l
            return true
    return false

line_array = array()

for line in file1
    line_array.add(line)

for line in file2
    if not line_exists(line, line_array)
        print(line)
klutt
  • 30,332
  • 17
  • 55
  • 95
  • Why not mention that the *line-oriented* functions in C already return either a pointer or `NULL` which would eliminate the need for `function line_exists()` to begin with? – David C. Rankin May 31 '19 at 07:02
  • @DavidC.Rankin That's an option. Create an own answer if you want. – klutt May 31 '19 at 07:18
0

This is the code that i finally used.

void CheckContent(char dir[100])
{

FILE *f1, *f2,*f3;
int a, b;
char dir2[100];
char temporal[100];

strcpy (temporal,"D:\\producto3\\temporal.txt");

printf("\nIntroduce la ruta del archivo donde estan los pares a leer.\n");
scanf("%s", &dir2); 


f1 = fopen(dir, "r");
f2 = fopen(dir2, "r");
f3 = fopen(temporal, "w");


if (f1 == NULL || f2 == NULL)
{
    printf("No se puede leer alguno de los ficheros");
}

else 
{
    char pairLine[256];
    f2 = fopen("D:\\producto3\\pares.txt", "r");

    while (fgets(pairLine, sizeof(pairLine), f2))
    {
        char hostLine[256];
        f1 = fopen("D:\\producto3\\hosts", "r");
        int counter = 0; 
        while (fgets(hostLine, sizeof(hostLine), f1))
        {
            if (hostLine[0] != '#')
            {
                if (strcmp(pairLine, hostLine) == 0)
                {                                   
                    counter++;                      
                }
            }
        }
        fclose(f1);
        if (counter == 0)
        {
            fputs(pairLine,f3);
        }
    }
    fclose(f3);
    fclose(f2);
}
FilePares(dir,temporal);
}