c# - 尽管解析字节数据时出错,如何继续循环

标签 c# .net parsing loops binaryreader

我的问题是这个问题的延续:( loop for reading different data types & sizes off very large byte array from file )

我有一个原始字节流存储在文件(rawbytes.txt 或 bytes.data)中,我需要解析它并将其输出到 CSV 样式的文本文件。

原始字节的输入(当读取为字符/长/整数等时)看起来像这样:

A2401028475764B241102847576511001200C...

解析后应该是这样的:

输出A.txt

(Field1,Field2,Field3) - heading

A,240,1028475764

输出B.txt

(Field1,Field2,Field3,Field4,Field5) - heading

B,241,1028475765,1100,1200

输出C.txt

C,...//and so on

本质上,它是连续的十六进制转储样式的字节输入,没有任何行终止符或需要解析的数据之间的间隙。如上所示,数据由一个接一个的不同数据类型组成。

这是我的代码片段 - 因为任何字段中都没有逗号,并且不需要使用“”(即 CSV 包装器),我只是使用 TextWriter 创建 CSV 样式的文本文件,如下所示:

if (File.Exists(fileName))
        {
        using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
        while (reader.BaseStream.Position != reader.BaseStream.Length)
            {
                inputCharIdentifier = reader.ReadChar();
                switch (inputCharIdentifier)
                     case 'A':

                        field1 = reader.ReadUInt64();
                        field2 = reader.ReadUInt64();
                        field3 = reader.ReadChars(10);
                        string strtmp = new string(field3);
                        //and so on
                        using (TextWriter writer = File.AppendText("outputA.txt"))
                        {
                            writer.WriteLine(field1 + "," + field2 + "," + strtmp); // +  
                        }
                        case 'B':
                        //code...

我的问题基于以下事实:某些原始字节数据包含空值,这些值很难解析 - 因为有 未知 数量的空字节(或非空,连续数据 block 之间的异位字节(如果数据 block 未损坏,则每个数据 block 均以 A、B 或 C 开头)。

问题

那么,如何添加默认情况或其他机制来继续循环,尽管可能因数据损坏或错误而出现错误?以下代码可以工作吗?

    inputCharIdentifier = reader.ReadChar();
    ...
    case default:
    //I need to know what to add here, instead of default 
    //(i.e. the case when the character could not be read)
    while (binReader.PeekChar() != -1)
    {
         filling = binReader.readByte();
         //filling is a single byte
         try {
             fillingChar = Convert.ToChar(filling);

             break;
         }
         catch (Exception ex) { break; }
         if (fillingChar == 'A' || fillingChar == 'B')
             break;

剩下的部分 - 向每个 switch case(例如“A”)添加代码以继续而不停止程序 - 有没有办法在没有多个 try-catch block 的情况下执行此操作? [IE。代码块字符标识符是 A,但 A 之后的字节已损坏 - 在这种情况下,我需要退出循环或读取(即跳过)定义的字节数 - 在这里,如果消息头正确标识,则可以知道剩余字节。

[注意:情况 A、B 等具有不同大小的输入 - 换句话说,A 总共可能是 40 字节,而 B 是 50 字节。因此,使用固定大小的缓冲区,例如 inputBuf[1000] 或 [50] - 如果它们大小相同 - 也不会很好地工作,AFAIK。]

有什么建议吗?请帮忙!我对 C# 还比较陌生(2 个月)...

更新:我的整个代码如下:

         class Program
{
    const string fileName = "rawbytes.txt";
    static void Main(string[] args)
    {
                    try
        {
            var program = new Program();
            program.Parser();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        Console.ReadLine();
    }
    public void Parser()
    {
        char inputCharIdentifier = 'Z';
        //only because without initializing inputCharIdentifier I ended up with an error
        //note that in the real code, 'Z' is not a switch-case alphabet
        //it's an "inconsequential character", i.e. i have defined it to be 'Z'
        //just to avoid that error, and to avoid leaving it as a null value
        ulong field1common = 0;
        ulong field2common = 0;
        char[] charArray = new char[10];
        char char1;
        char char2;
        char char3;
        int valint1 = 0;
        int valint2 = 0;
        int valint3 = 0;
        int valint4 = 0;
        int valint5 = 0;
        int valint6 = 0;
        int valint7 = 0;
        double valdouble;
        /*
        char[] filler = new char[53];
        byte[] filling = new byte[4621];
        byte[] unifiller = new byte[8];
        //these values above were temporary measures to manually filter through
        //null bytes - unacceptable for the final program
        */
        if (File.Exists(fileName))
        {
            using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                while (reader.BaseStream.Position != reader.BaseStream.Length)
                {
                    //inputCharIdentifier = reader.ReadChar();
                    //if (inputCharIdentifier != null)
                    //{
                        try
                        {
                            inputCharIdentifier = reader.ReadChar();
                            try
                            {
                                switch (inputCharIdentifier)
                                {
                                    case 'A':

                                        field1common = reader.ReadUInt64();
                                        field2common = reader.ReadUInt64();
                                        //unifiller = reader.ReadBytes(8);
                                        //charArray = reader.ReadString();
                                        //result.ToString("o");
                                        //Console.WriteLine(result.ToString());
                                        charArray = reader.ReadChars(10);
                                        string charArraystr = new string(charArray);
                                        char1 = reader.ReadChar();
                                        valint1 = reader.ReadInt32();
                                        valint2 = reader.ReadInt32();
                                        valint3 = reader.ReadInt32();
                                        valint4 = reader.ReadInt32();
                                        using (TextWriter writer = File.AppendText("A.txt"))
                                        {
                                            writer.WriteLine(field1common + "," + /*result.ToString("o")*/ field2common + "," + charArraystr + "," + char1 + "," + valint1 + "," + valint2 + "," + valint3 + "," + valint4);
                                            writer.Close();
                                        }
                                        break;


                                    case 'B':
                                    case 'C':

                                        field1common = reader.ReadUInt64();
                                        field2common = reader.ReadUInt64();
                                        //charArray = reader.ReadString();
                                        charArray = reader.ReadChars(10);
                                        string charArraystr2 = new string(charArray);
                                        char1 = reader.ReadChar();
                                        valint1 = reader.ReadInt32();
                                        valint2 = reader.ReadInt32();
                                        using (TextWriter writer = File.AppendText("C.txt"))
                                        {
                                            writer.WriteLine(field1common + "," + result2.ToString("o") + "," + charArraystr2 + "," + char1 + "," + valint1 + "," + valint2);
                                            writer.Close();
                                        }
                                        break;
                                    case 'S':
                                        //market status message
                                        field1common = reader.ReadUInt64();
                                        char2 = reader.ReadChar();
                                        char3 = reader.ReadChar();
                                        break;
                                    case 'L':
                                        filling = reader.ReadBytes(4);
                                        break;
                                    case 'D':
                                    case 'E':
                                        field1common = reader.ReadUInt64();
                                        field2common = reader.ReadUInt64();
                                        //charArray = reader.ReadString();
                                        charArray = reader.ReadChars(10);
                                        string charArraystr3 = new string(charArray);
                                        //char1 = reader.ReadChar();
                                        valint1 = reader.ReadInt32();
                                        valint2 = reader.ReadInt32();
                                        valint5 = reader.ReadInt32();
                                        valint7 = reader.ReadInt32();
                                        valint6 = reader.ReadInt32();
                                        valdouble = reader.ReadDouble();
                                        using (TextWriter writer = File.AppendText("D.txt"))
                                        {
                                            writer.WriteLine(field1common + "," + result3.ToString("o") + "," + charArraystr3 + "," + valint1 + "," + valint2 + "," + valint5 + "," + valint7 + "," + valint6 + "," + valdouble);
                                            writer.Close();
                                        }
                                        break;
                                    }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Parsing didn't work");
                                Console.WriteLine(ex.ToString());
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Here's why the character read attempt didn't work");
                            Console.WriteLine(ex.ToString());

                            continue;
                            //continue;
                        }
                    //}
                }
            }
            }
            }

我收到的错误如下:

    Here's why the character read attempt didn't work

    System.ArgumentException: The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'System.Text.DecoderReplacementFallback'.
    Parameter name: chars
    at System.Text.Encoding.ThrowCharsOverflow()
    at System.Text.Encoding.ThrowCharsOverflow(DecoderNLS decoder, Boolean nothingDecoded)
    at System.Text.UTF8Encoding.GetChars(Byte* bytes, Int32 byteCount, Char* chars, Int32 charCount, DecoderNLS baseDecoder)
    at System.Text.DecoderNLS.GetChars(Byte* bytes, Int32 byteCount, Char* chars, Int32 charCount, Boolean flush)
    at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex, Boolean flush)
    at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex)
    at System.IO.BinaryReader.InternalReadOneChar()
    at System.IO.BinaryReader.Read()
    at System.IO.BinaryReader.ReadChar()
    at line 69: i.e. inputCharIdentifier = reader.ReadChar();

更新:生成上述相同错误的示例文件位于以下链接: http://www.wikisend.com/download/106394/rawbytes.txt

特别注意连续数据 block 之间的 8 个意外空字节,即使数据 block header - 即 inputCharIdentifier - 有效。此类 header 后面的字节数总是不可预测并且通常会变化。我的问题是,当出现下一个可用的未损坏数据 block 时,我需要能够删除或跳过这种情况 - 在示例文件的情况下,是在8 个不合适的空字节。

8 个空字节可以在文件中定位,如下所示: 字节计数器:1056 第2行,第783列(根据Notepad++)

问题的关键在于 8 个空字节可以是任意大小 - 3、7、15、50 等。它始终是未知的 - 作为数据损坏的直接结果。但与“传统”数据损坏不同,即数据 block 内固定数量的字节(例如 50 个)可能不可读,因此可以跳过(按确切的字节数) - 我面临的数据损坏由有效数据 block 之间的字节数未知。

最佳答案

您无法为这些情况分配案例,因为目标变量 (inputCharIdentifier) 为 null;因此,有一个条件就足以避免这些情况。我还添加了一个 try...catch,只是为了完全确定(执行所有给定操作时的任何错误都会使代码自动跳到下一个迭代)。

try
{
    using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open), Encoding.ASCII))
    {
        while (reader.BaseStream.Position != reader.BaseStream.Length)
        {
            inputCharIdentifier = reader.ReadChar();
            if(inputCharIdentifier != null)
            {
               switch (inputCharIdentifier)
                 case 'A':
                    field1 = reader.ReadUInt64();
                    field2 = reader.ReadUInt64();
                    field3 = reader.ReadChars(10);
                    string strtmp = new string(field3);
                    //and so on
                    using (TextWriter writer = File.AppendText("outputA.txt"))
                    {
                       writer.WriteLine(field1 + "," + field2 + "," + strtmp); 
                    }
                 case 'B':
                   //code...
            }
        }
    }
}
catch
{
}

关于c# - 尽管解析字节数据时出错,如何继续循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17188341/

相关文章:

.net - 我无法在我的 Web 应用程序 :( 中使用 log4net

c# - Linq-to-Entities 查询中的动态条件

http - Web 服务器 - 如何解析请求?异步流标记器?

c# - 如何从基类中获取属性值?

c# - 在 .NET 中如何检查事件是否已被订阅?

c# - 使用 IQueryable 查找另一个坐标的半径内的点

c# - WCF 服务对象序列化

c# - 上传大文件

c# - 用引号解析命令行参数

python - BeautifulSoup,你要把我的 HTML 放在哪里?