在C#中读取顺序二进制文件的内容 [英] Read the contents of sequential Binary File in C#

查看:241
本文介绍了在C#中读取顺序二进制文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





任何人都可以帮助我。问题:

我需要从linux中顺序生成的二进制文件中读取局部变量中的数据。我很难从二进制文件中读取int,float和double值。因为它们在作为流读取时生成单独的输出并按顺序读取。



到目前为止我尝试过的内容:

我已经尝试使用BinaryFormatter将二进制文件转换为可读流,但它会抛出输入流不是有效二进制格式的异常。起始内容(以字节为单位)为:26- 41-6B-73-68-61-79-20-20-20-20-20-20-20-20-20-20 ......



我甚至尝试过BinaryReader,但只能读取字符串/字符,但数字不同,与声明的类型无关。



StreamReader不好用这种情况..这是我的假设,因为StreamReader将读取流而不是按顺序读取。



请帮助..谢谢

Hi,

Can anyone help me in the foll. issue:
I need to read data in local variables from a binary file generated sequentially in linux. It is very much difficult for me to read the int's, float's and double values from the binary file. As they generate separate outputs when read as stream and read as sequential.

What I have tried so far:
I have tried BinaryFormatter to convert the binary file to readable stream, but it throws an exception of "The input stream is not a valid binary format. The starting contents (in bytes) are: 26-41-6B-73-68-61-79-20-20-20-20-20-20-20-20-20-20 ..."

I even tried BinaryReader, but only able to read strings/chars, but the numbers come different and have no relation the type declared.

The StreamReader is no good in this case.. This is my assumption as StreamReader will read a stream and not sequentially as I want.

Please help.. Thanks

推荐答案

所有3个值都写在同一行,并以制表符分隔

你在哪里阅读标签?无处。在每次调用br.ReadSomething()之后,您必须将位置增加一个额外的字节。 br.ReadByte()可以提供帮助。 - 在最后一次读取操作后不要这样做。

只计算:

30(字符串)+ 1(第一个标签)+ 4(用于float)+ 1(对于第二个选项卡)+ 4(对于int)= 40个字节。
"All the 3 values are written in same line with tab delimited"
And where do you read the tabs? Nowhere. After every br.ReadSomething() call you must advance the position by an additional byte. A br.ReadByte() could help. - Do not do that after the last read operation.
And just calculate:
30 (for the string) + 1 (for the first tab) + 4 (for the float) + 1 (for the second tab) + 4 (for the int) = 40 bytes.


您确实需要知道文件格式的所有细节。阅读非常简单:

http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readdouble(v = vs.110).aspx [ ^ ],

http://msdn.microsoft .com / zh-cn / library / system.io.binaryreader.readsingle(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system .io.binaryreader%28v = vs.110%29.aspx [ ^ ]。



如果你在每个数字或任何其他数字的偏移中犯了一个小错误田野,你开始在田野之间阅读,并在这种转变后搞砸了所有东西。所以,你需要准确。



-SA
You really need to know all the detail of the file format. Reading is quite simple:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readdouble(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readsingle(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binaryreader%28v=vs.110%29.aspx[^].

If you make a tiny mistake in an offset of each numeric or any other field, you start to read between fields and screw up everything after such shift. So, you need to be accurate.

—SA


这篇关于在C#中读取顺序二进制文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆