投稿者 ケンケン  (社会人) 投稿日時 2022/10/18 16:50:14
C# バイナリーファイルの読み込みの繰り返しが上手く行かない。

   構造体の内容です。 

        [StructLayout(LayoutKind.Sequential)]    // フィールドを順にシリアライズ
        public struct usrData
        {

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string id;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string accID;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string name;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string profileID;

            public long revDate;

            public int userLevel;

            public usrData(string ID, string AccID, string Name, string ProfileID, long RevData, int UserLevel)
            {

                id = ID;
                accID = AccID;
                name = Name;
                profileID = ProfileID;

                revDate = RevData;
                userLevel = UserLevel;
            }

        }

        ↓は実行処理

        private void button16_Click(object sender, EventArgs e)
        {
            string dataPath = Directory.GetCurrentDirectory() + @"\data\";

            FileStream fs = new FileStream(dataPath + "Mydata.data", FileMode.Open, FileAccess.Read);

            //ファイルのバイト長だけバイト配列を作成
            byte[] bs = new byte[fs.Length];

            for (; ; )
            {
                int readSize = fs.Read(bs, 0, bs.Length);

                Console.WriteLine("readSize :" + readSize);

                if (readSize == 0)
                {
                    break;
                }
                //ファイルの内容を読み込む
                fs.Read(bs, 0, bs.Length);

                Console.WriteLine("bs.length " + bs.Length);

                //ファイルが無い場合は終了する
                Form1.usrData Mydata;

                int size = bs.Length;
                IntPtr ptr = Marshal.AllocHGlobal(size);
                Marshal.Copy(bs, 0, ptr, size);
                Mydata = (Form1.usrData)Marshal.PtrToStructure(ptr, typeof(Form1.usrData));
                Marshal.FreeHGlobal(ptr);


                Console.WriteLine("id   :" + Mydata.id);
                Console.WriteLine("acc  :" + Mydata.accID);
                Console.WriteLine("name :" + Mydata.name);
                Console.WriteLine("prof :" + Mydata.profileID);
                Console.WriteLine("rev  :" + Mydata.revDate);
                Console.WriteLine("level:" + Mydata.userLevel);
            }

            fs.Close();

            Console.WriteLine("読み込み改終了");

        }

  ※ 3件のデータが有りますが、1件読んで終わってしまいます。
     繰り返して3件分のデータが表示されません。

    何方か、わかる方ご教授お願いいたします。