gpt4 book ai didi

C# 相当于 fread

转载 作者:行者123 更新时间:2023-11-28 01:01:07 24 4
gpt4 key购买 nike

我正在将一些 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 个字节的数据。

最佳答案

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

关于C# 相当于 fread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8632974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com