6

我喜欢扫描较大(> 500M)的二进制文件以查找结构/模式。我是这门语言的新手,希望有人能给我开始。实际上,这些文件是一个包含 Segments 的数据库。段以固定大小的标头开始,后跟固定大小的可选部分,然后是可变长度的有效负载/数据部分。对于第一次测试,我只想记录文件中的段数。我已经用谷歌搜索了一些教程,但没有发现任何帮助。我需要一个与我的用例相距不远的提示或教程才能开始。

问候斯特凡

4

3 回答 3

4

您需要了解位语法二进制理解。更多有用的链接:http ://www.erlang.org/documentation/doc-5.6/doc/programming_examples/bit_syntax.html和http://goto0.cubelogic.org/a/90

您还需要学习如何处理文件、读取文件(逐行、逐块、在文件中的给定位置等),以多种方式写入文件。文件处理功能在这里

解释您也可以选择查看erlang包中的大型文件处理库的源代码,例如Disk LogDetsmnesia. 这些库对文件进行大量读写,并且它们的源代码是开放的,供您查看。

我希望这会有所帮助

于 2012-06-13T12:20:57.693 回答
1

这是一个综合示例问题:我有一个要解析的二进制文件 ( test.txt )。我想找到<<$a, $b, $c>>文件中的所有二进制模式。

test.txt ”的内容:

I arbitrarily decide to choose the string "abc" as my target string for my test. I want to find all the abc's in my testing file.

一个示例程序(lab.erl):

-module(lab).
-compile(export_all).

find(BinPattern, InputFile) ->
    BinPatternLength = length(binary_to_list(BinPattern)),
    {ok, S} = file:open(InputFile, [read, binary, raw]),
    loop(S, BinPattern, 0, BinPatternLength, 0),
    file:close(S),
    io:format("Done!~n", []).

loop(S, BinPattern, StartPos, Length, Acc) ->
    case file:pread(S, StartPos, Length) of
    {ok, Bin} ->
        case Bin of
        BinPattern ->
            io:format("Found one at position: ~p.~n", [StartPos]),
            loop(S, BinPattern, StartPos + 1, Length, Acc + 1);
        _ ->
            loop(S, BinPattern, StartPos + 1, Length, Acc)
        end;
    eof ->
        io:format("I've proudly found ~p matches:)~n", [Acc])
    end.

运行:

1> c(lab).
{ok,lab}
2> lab:find(<<"abc">>, "./test.txt").     
Found one at position: 43.
Found one at position: 103.
I've proudly found 2 matches:)
Done!
ok

请注意,上面的代码效率不高(扫描过程一次移动一个字节)并且它是顺序的(没有利用计算机上的所有“核心”)。它只是为了让你开始。

于 2012-06-14T08:26:58.403 回答
1

When your data fits into memory, best thing what you can to do is read data in whole using file:read_file/1. If you can't use file in raw mode. Then you can parse data using bit_syntax. If you write it in right manner you can achieve parsing speed in tens of MB/s when parsing module is compile using HiPE. Exact techniques of parsing depends on exact segment data format and how robust/accurate result you are looking for. For parallel parsing you can inspire by Tim Bray's Wide Finder project.

于 2012-06-16T14:01:59.667 回答