你應該提供更多關於你想要做什麼以及到目前爲止嘗試過的細節。
我加入一個執行比較的代碼示例,用行號打印不同的行,如果一個文件更長,則打印剩餘的行。
我不會將二進制文件轉換爲列表,這是不必要的和低效的。
-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").
一個小測試:
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>
您可以使用模式匹配來比較兩個二進制文件。 –
但我還需要顯示不同的值 –
您是否想要顯示兩個二進制文件之間的差異? –