Comparing two .txt files

It has been a while since I have done any programming in anything other than G code since I have been working as a machine shop engineer.

I need to perform a simple task, compare two text files character by character and scan for differences. The data is machine parameters from a CNC machine, since it has been having issues lately. I want to find a program that will take the two text files and compare them/output what is different if anything.

N0001 P 01001000
N0002 P 11000011
N0003 P 00000001
N0004 P 01110101
N0005 P 00110101
N0006 P 00000000
N0007 P 00000000
N0008 P 00000000
N0009 P 00010001
N0010 P 11100000
N0011 P 00000000
N0012 P 10000001

vs

N0001 P 01001000
N0002 P 11000011
N0003 P 00000001
N0004 P 01110101
N0005 P 00110101
N0006 P 00000000
N0007 P 00000000
N0008 P 00000000
N0009 P 00010001
N0010 P 11100001
N0011 P 00000000
N0012 P 10000001

Thanks!
-Justin
Last edited on
there are TONS of programs that do this already, with the smarts to resync after a difference (so if file 2 has 3 lines that file 1 does not have in the middle of it, it won't say everything after the first difference is a change, it reconnects and resumes the matched parts). One of those is windiff. A fancy one that costs money is beyond compare. Commandline build into both OS exists as well, for windows its the fc command. ALL version control software like git have these tools as well.

if you want to do it yourself, do you want to resync or just hit the first difference? I don't recommend doing it yourself unless you absolutely must, but if you want to, we can certainly help.
Last edited on
Are you on a Linux machine? If so, then check out the 'diff' command. Very good for this type of thing.
Last edited on
Even if you are not on a Linux machine, there are versions of diff available on Windows too. For example
https://gnuwin32.sourceforge.net/packages/diffutils.htm
Last edited on
For windows, there's the in-built windiff program
For windows, there's the in-built windiff program

I don't seem to have that built-in.
Last edited on
WinDiff was dropped from the Win SDK back with Win 8/VS 2012, the last stable release was May 2010.

There are unofficial uploads available.
the old one still works fine, if unofficial does not suit you.
Visual studio has the functionality built in, but its clunky to access.
notepad ++ has plugins that do it.
Last edited on
I have 'diff' in WSL. Previously on MSYS. Even used 'ediff' mode of GNU Emacs on Windows -- as long as diffutils were present.
Default:
$ diff first.txt second.txt
10c10
< N0010 P 11100000
---
> N0010 P 11100001

Side-by-side:
$ diff -y first.txt second.txt
N0001 P 01001000	N0001 P 01001000
N0002 P 11000011	N0002 P 11000011
N0003 P 00000001	N0003 P 00000001
N0004 P 01110101	N0004 P 01110101
N0005 P 00110101	N0005 P 00110101
N0006 P 00000000	N0006 P 00000000
N0007 P 00000000	N0007 P 00000000
N0008 P 00000000	N0008 P 00000000
N0009 P 00010001	N0009 P 00010001
N0010 P 11100000   |	N0010 P 11100001
N0011 P 00000000	N0011 P 00000000
N0012 P 10000001	N0012 P 10000001



I recall a British TV series from 80's or early 90's. "Programmer" printed the two datasets ("code") on transparencies and superimposed them. Identical text was clear, changed byte was not. (Only some bytes had changed, no insertions.)
While we're on the topic, winmerge and p4merge can pleasantly/graphically show diffs between files.
p4merge file1 file2
Hello. I was busy - and I could not be active on the forum. Maybe it is too late, but I would like to show you a solution that could be useful - as a first step. It works as expected, checking two vectors with all words. If you can formulate your entries differently like N0001*P*01001000, you will be able to compare each line. In my example, it compares each word (returning its index too). You can easily modify this previous code according to your project ++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

class readFile {

private:
    std::vector<std::string> log;

public:
    readFile(std::string myFile)
    {
        std::string buffer;
        std::ifstream input_file(myFile);

        if (!input_file.is_open())
        {
            std::cout << "Could not open the file " << myFile << std::endl;
            exit(EXIT_FAILURE);
        }

        while (input_file >> buffer)
            log.push_back(buffer);

        input_file.close();
    }

    size_t getSize() { return log.size(); }
    std::string getLine(size_t index){ return log[index]; }
};

int main()
{
    size_t maxSize;

    readFile txt1("log1.txt");
    readFile txt2("log2.txt");

    txt1.getSize() <= txt2.getSize() ? maxSize = txt1.getSize() : maxSize = txt2.getSize();
   
    for (size_t x = 0; x < maxSize; ++x)
        if (txt1.getLine(x) != txt2.getLine(x))
            std::cout << "this value is not the same : " << txt1.getLine(x) << " " << txt1.getLine(x - 2) << std::endl;

    return 0;
}

this value is not the same : 11100000 N0010
Last edited on
Or this one which concatenate strings - 3 words for an entry in the vector log ++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

class readFile {

private:
    std::vector<std::string> log;
    int o = 1;

public:
    readFile(std::string myFile)
    {
        std::string buffer;
        std::string b;
        std::ifstream input_file(myFile);

        if (!input_file.is_open())
        {
            std::cout << "Could not open the file " << myFile << std::endl;
            exit(EXIT_FAILURE);
        }

        while (input_file >> buffer)
        {
            if (o > 2)
            {
                log.push_back(b);
                o = 0;
                b.erase();
            }
            else {
                b.append(buffer);
                b.append(" ");
                ++o;
            }
        }

        input_file.close();
    }

    size_t getSize() { return log.size(); }
    std::string getLine(size_t index){ return log[index]; }
};

int main()
{
    size_t maxSize;

    readFile txt1("log1.txt");
    readFile txt2("log2.txt");

    txt1.getSize() <= txt2.getSize() ? maxSize = txt1.getSize() : maxSize = txt2.getSize();
   
    for (size_t x = 0; x < maxSize; ++x)
        if (txt1.getLine(x) != txt2.getLine(x))
            std::cout << "this line is not the same : " << txt1.getLine(x) << std::endl;

    return 0;
}

this line is not the same : N0010 P 11100000
Last edited on
Topic archived. No new replies allowed.