Sie sollten mehr Details darüber geben, was Sie tun möchten und was Sie bisher versucht haben.
Ich schließe mich einem Beispielcode an, der den Vergleich durchführt, die verschiedenen Zeilen mit der Zeilennummer druckt, die restlichen Zeilen druckt, wenn eine Datei länger ist.
Ich transformiere Binärdateien nicht in eine Liste, es ist unnötig und ineffizient.
-module (comp).
-export ([compare/2]).
compare(F1,F2) ->
{ok,B1} = file:read_file(F1),
{ok,B2} = file:read_file(F2),
io:format("compare file 1: ~s to file 2 ~s~n",[F1,F2]),
compare(binary:split(B1, [<<"\n">>], [global]),
binary:split(B2, [<<"\n">>], [global]),
1).
compare([],[],_) ->
io:format("the 2 files have the same length~n"),
done();
compare([],L,N) ->
io:format("----> file 2 is longer:\n"),
print(L,N);
compare(L,[],N) ->
io:format("----> file 1 is longer:\n"),
print(L,N);
compare([X|T1],[X|T2],N) -> compare(T1,T2,N+1);
compare([X1|T1],[X2|T2],N) ->
io:format("at line ~p,~nfile 1 is: ~s~nfile 2 is: ~s~n",[N,X1,X2]),
compare(T1,T2,N+1).
print([],_) -> done();
print([X|T],N) ->
io:format("line ~p: ~s~n",[N,X]),
print(T,N+1).
done() -> io:format("end of comparison~n").
ein kleiner Test:
1> c(comp).
{ok,comp}
2> comp:compare("../doc/sample.txt","../doc/sample_mod.txt").
compare file 1: ../doc/sample.txt to file 2 ../doc/sample_mod.txt
at line 9,
file 1 is: Here's an example:
file 2 is: Here's an example (modified):
at line 22,
file 1 is: ```
file 2 is: ```
----> file 2 is longer:
line 23:
line 24: Extra text...
line 25:
end of comparison
ok
3> comp:compare("../doc/sample.txt","../doc/sample.txt").
compare file 1: ../doc/sample.txt to file 2 ../doc/sample.txt
the 2 files have the same length
end of comparison
ok
4>
können Sie Pattern-Matching verwenden zwei Binärdateien zu vergleichen. –
aber ich muss auch verschiedene Werte anzeigen –
Möchten Sie auch den Unterschied zwischen zwei Binärdateien anzeigen? –