![](/img/trans.png)
[英]I'm trying to read a Regex from an xml file but when i pass on the Regex to the C# code but I'm getting false matches
[英]I'm trying to read from a custom file type in C#
我正在嘗試從創建的自定義文件類型讀取和寫入,如下所示:
public static byte[] writeTo(Structures.SearchDS s)
{
var o = new MemoryStream();
var b = new BinaryWriter(o);
b.Write(s.magic);
b.Write(s.name);
b.Write(s.age);
b.Write(s.b.ElementAt(0).Houseno);
b.Write(s.b.ElementAt(0).location);
return o.ToArray();
}
public static Structures.SearchDS readSearchFile(byte[] a)
{
MemoryStream ms = new MemoryStream(a);
BinaryReader br = new BinaryReader(ms);
Structures.SearchDS ss = new Structures.SearchDS();
ss.magic=br.ReadChars(5);
ss.name = br.ReadString();
ss.age = br.ReadUInt16();
ss.b[0] = new Structures.House();
ss.b[0].Houseno = br.ReadString();
ss.b[0].location = br.ReadString();
return ss;
}
主要方法:
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
// TODO: Implement Functionality Here
byte[] testFile=Tools.writeTo(Tools.adding());
File.WriteAllBytes("test3.search", testFile);
Structures.SearchDS ss1 = Tools.Write(File.ReadAllBytes("test.search"));
Console.WriteLine(ss1.age);
Console.WriteLine(ss1.name);
Console.WriteLine(ss1.magic);
ss1.b[0] = new Structures.House();
Console.WriteLine(ss1.b.ElementAt(0).Houseno);
Console.WriteLine(ss1.b.ElementAt(0).location);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
但我不斷收到異常:
流結束異常
在
ss.name = br.ReadString();
我使用十六進制編輯器打開了文件,然后看到數據正確寫入,並且文件流同時出現以下異常
“ ms.WriteTimeout”引發了“ System.InvalidOperationException”類型的異常
'ms.ReadTimeout'引發了'System.InvalidOperationException'類型的異常
我的數據結構是:
public class SearchDS
{
public char[] magic = { 'S', 'E', 'A', 'R', 'C', 'H' };
public string name;
public UInt16 age;
public House[] b = new House[1];
}
public class House { public string Houseno; public string location; }
您使用“ SEARCH”的六個字符初始化了magic
,但是使用5調用了ReadChars
。 這將導致下一個讀取的字符串不正確。 ReadString
嘗試獲取要讀取的長度,該長度將使用char H初始化(部分長度...字符串為Unicode)。 該長度超出了您可以使用的剩余長度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.