C#相当于fread

时间:2011-12-26 04:47:49

标签: c# c++

我正在将一些C ++代码转换为C#,我试图找出如何在C#应用程序中编写和遵循C ++代码并让它做同样的事情:

fread(&Start, 1, 4, ReadMunge); //Read File position

我尝试了多种方法,例如使用FileStream:

        using (FileStream fs = File.OpenRead("File-0027.AFS"))
        {
            //Read amount of files from offset 4
            fs.Seek(4, SeekOrigin.Begin);
            FileAmount = fs.ReadByte();
            string strNumber = Convert.ToString(FileAmount);
            fileamountStatus.Text = strNumber;

            //Seek to beginning of LBA table
            fs.Seek(8, SeekOrigin.Begin);
            CurrentOffset = fs.Position;

            int numBytesRead = 0;

            while (Loop < FileAmount) //We want this to loop till it reachs our FileAmount number
            {
                Loop = Loop + 1;

                //fread(&Start, 1, 4, ReadMunge); //Read File position
                //Start = fs.ReadByte();
                //Size = fs.ReadByte();
                CurrentOffset = fs.Position;
                int CurrentOffsetINT = unchecked((int)CurrentOffset);

                //Start = fs.Read(bytes,0, 4);
                Start = fs.Read(bytes, CurrentOffsetINT, 4);
                Size = fs.Read(bytes, CurrentOffsetINT, 4);


                Start = fs.ReadByte();


            }
        }

我一直遇到的问题是Start/Size不能保存我需要的4个字节的数据。

1 个答案:

答案 0 :(得分:3)

如果您正在阅读二进制文件,则可能应该考虑使用BinaryReader。这样您就不必担心将字节数组转换为整数或其他内容。例如,您可以简单地调用reader.ReadInt32来读取int。