2011-12-26 152 views
0

的我在一些C++代碼轉換爲C#中,我試圖找出我怎麼能寫出來,在我的C#應用​​程序下面的C++代碼和過程有它做同樣的事情:C#相當於FREAD

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個字節的數據,我需要。

回答

3

如果你正在閱讀一個二進制文件,你可能應該看看使用BinaryReader。這樣你就不必擔心把字節數組轉換成整數或其他東西。例如,您可以簡單地調用reader.ReadInt32來讀取int。