2014-02-11 108 views
2

我寫在二進制文件中的整數值如下: - 檢索如何從二進制文件中檢索整數值在C#

int val =10; 

FileStream fs = new FileStream("BinaryFile.bin", FileMode.Create); 
      BinaryWriter bw = new BinaryWriter(fs, Encoding.Unicode); 

bw.Write(val); 

//Reading value from binary as:- 

FileStream fs = new FileStream("BinaryFile.bin", FileMode.Open); 
    BinaryReader br = new BinaryReader(fs, Encoding.Unicode); 

int x = br.ReadInt32(); 

值爲:1.092616E + 09

我得到這個值而不是「10」

是否有任何其他方法來檢索int值?

+2

工作正常,我讀出。你如何顯示'x'? –

+0

我只是在消息框中顯示x。 – Payalg

+0

當您調試代碼並跨越'ReadInt32'行時,會發生什麼?'x'是否持有'10'? –

回答

0

試圖通過使變化的BinaryWriter構造

FileStream fs = new FileStream("iram.bin", FileMode.Create); 
     // Create the writer for data. 
     BinaryWriter w = new BinaryWriter(fs); 

w.Write((int) 2000); 

w.Close(); 
fs.Close(); 

,並使用

using (FileStream fs2 = new FileStream("iram.bin", FileMode.Open)) 
    { 
     using(BinaryReader r = new BinaryReader(fs2)) 
     { 
      var integerValue = r.ReadInt32(); 
     } 
    } 

更多細節Writing to .bin binary file

+0

謝謝!這是行得通的。 – Payalg

相關問題