开发者

C# from read text file to read binary file

how do I modify this code to read binary file using BinaryReader? Example snort's log file?(text and number are include)

public string ReadFullFile()
{
    using (StreamRe开发者_开发技巧ader streamReader = new StreamReader(this.filename))
    {
        return streamReader.ReadToEnd();
    }
}


I don't know about snort's log, but binary reader goes something like this:

class Record
{
    public int Id { get; set; }
    public string Name { get; set; }
}
function ReadFullFile(Action<Record> processRecord)
{
    using(var file = new FileStream("whatever.bin"))
    {
        using(var reader = new BinaryReader(file))
        {
            processRecord(new Record
            {
                Id = reader.ReadInt32(),
                Name = reader.ReadString(),
            });
        }
    }
}


public byte[] ReadFullFile()
{
    return File.ReadAllBytes(this.FileName);
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜