locked
Write multidimensional array to a file with a BinaryWriter RRS feed

  • Question

  • Hi

    Anyone who can explain me how to write a multidimensional array to a file with a BinaryWriter? I mean the following problem:

    public void SaveAs(string fn)  
        {  
          byte[,] M = new byte[256, 256];  
          //  
          // Fill M  
          //  
     
          FileStream fs = null;  
          BinaryWriter bw = null;  
     
          try  
          {  
            fs = new FileStream(fn, FileMode.Create, FileAccess.Write);  
            bw = new BinaryWriter(fs);  
     
            // follow code gives an error  
            bw.Write(M,0,256*256 );  
          }  
          catch (Exception ex)  
          {  
            throw new Exception("Threshold.SaveAs err (" + ex.Message + ")");  
          }  
          finally  
          {  
            if (bw != null)  
              bw.Close();  
            if (fs != null)  
              fs.Close();  
          }  
        } 

    Thx in advance

    Monday, November 10, 2008 3:48 PM

Answers

  • Hi,

    you can use serialization to do this:

    using System.Runtime.Serialization.Formatters.Binary;   
     
    int[,] array = // initialize multi-dimensional array. 
    string fileName = "file.dat";    
    BinaryFormatter bf = new BinaryFormatter();   
    FileStream fs = new FileStream(filaName, FileMode.Create, FileAccess.Write);   
    bf.Serialize(fs, array); 

    // close streams...


    Tomi Airaksinen - MCPD [Remember to click "mark as answered" when you get a correct reply to your question]
    Monday, November 10, 2008 5:38 PM
  • conrarn said:

    This may not be the perfect solution but I think you will get what I am trying to do.

            for (int i = 0; i < 256; i++)  
            {  
              for (int j = 0; j < 256; j++)  
              {  
                bw.Write(M[i,j],0,1);  
              }    
            }     
     



    Hi

    That's the solution I'm using for the moment, but I'm looking for something faster.

    Thanks anyway
    Kind regards
    Tuesday, November 11, 2008 10:49 AM

All replies

  • Hi,

    you can use serialization to do this:

    using System.Runtime.Serialization.Formatters.Binary;   
     
    int[,] array = // initialize multi-dimensional array. 
    string fileName = "file.dat";    
    BinaryFormatter bf = new BinaryFormatter();   
    FileStream fs = new FileStream(filaName, FileMode.Create, FileAccess.Write);   
    bf.Serialize(fs, array); 

    // close streams...


    Tomi Airaksinen - MCPD [Remember to click "mark as answered" when you get a correct reply to your question]
    Monday, November 10, 2008 5:38 PM
  • This may not be the perfect solution but I think you will get what I am trying to do.

            for (int i = 0; i < 256; i++)  
            {  
              for (int j = 0; j < 256; j++)  
              {  
                bw.Write(M[i,j],0,1);  
              }    
            }     
     
    • Proposed as answer by conrarn Saturday, November 15, 2008 2:36 AM
    Tuesday, November 11, 2008 4:49 AM
  • Hi

    Thanks for your reply. I'll try it ...

    kind regards
    Tuesday, November 11, 2008 10:47 AM
  • conrarn said:

    This may not be the perfect solution but I think you will get what I am trying to do.

            for (int i = 0; i < 256; i++)  
            {  
              for (int j = 0; j < 256; j++)  
              {  
                bw.Write(M[i,j],0,1);  
              }    
            }     
     



    Hi

    That's the solution I'm using for the moment, but I'm looking for something faster.

    Thanks anyway
    Kind regards
    Tuesday, November 11, 2008 10:49 AM
  • Any time taken is going to be dominated by the disk write speed. A binary writer is buffered, so the disk writes themselves should already be pretty optimized. I think it unlikely that you'll be able to noticably speed things up.
    Tuesday, November 11, 2008 3:23 PM
  • I know this thread is waaaayyy old, but discovered it while looking to solve a similar problem.

    The BinnaryFormatter is not available on the compact framework. So that solution can't work for XNA or mobile products.

    thanks

    Tuesday, March 22, 2011 12:08 PM