你应该提供更多关于你想要做什么以及到目前为止尝试过的细节。
我加入一个执行比较的代码示例,用行号打印不同的行,如果一个文件更长,则打印剩余的行。
我不会将二进制文件转换为列表,这是不必要的和低效的。
-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>
您可以使用模式匹配来比较两个二进制文件。 –
但我还需要显示不同的值 –
您是否想要显示两个二进制文件之间的差异? –