M
M
MuffinLover2022-04-21 19:55:27
C++ / C#
MuffinLover, 2022-04-21 19:55:27

How to do it correctly? File comparison?

The trick is: you need to make a program that makes a difference between two files ... If text files are input, then character by character, and if binary, then byte by character ..
If character by character, then you need to output the line in which the first mismatch and display a plus between the lines where the difference is . You need to output in this format: example
abcd
++
attd

And let's suppose there are two files:
Let's say there are two files:
1)
```
tttt
tttt
abcd
2)
```
tttt
tttt
attd
```
It is necessary to output
abcd
++
attd

How to do it correctly? Calculate offset when the mismatch started (relative to the read buffer) and print starting from there? or how? How nice would you do

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2022-04-21
@Nipheris

https://github.com/git/git/blob/master/diff.c

R
res2001, 2022-04-22
@res2001

Calculate offset

Nothing needs to be calculated. Compare character-by-character without using strcmp (or memcmp) and you'll have the current index as offset. Also, you don't need a "global" offset for the text version, because you will compare line by line. Those. offset only within this particular line.
For a binary comparison, everything is the same, but the offset will be global, here the CR and LF characters do not play a role.
Keep in mind that in Windows the line feed is CRLF (0x0D 0x0A), and in nix LF (0x0A)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question