Write String array to Binary File

I am writing a program to write a heap (multi dimension string array) to a binary file.  Below is the code i wrote to create the binary text file.  How would i go about doing that.  The heap array is called string [,] HeapHold.
static void SaveHeap()// called by Main
        {
            BinaryWriter HeapInput = new BinaryWriter(new FileStream("HeapFile.txt", FileMode.OpenOrCreate));
 
            for (int i = 1; i <= N; i++)
            {
                HeapInput.Write(?????);
            }
 
            WriteToLog.WriteLine("*SaveHeap done - Head has __ nodes");
            WriteToLog.WriteLine();
        }

Open in new window

jmkotmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JimBrandleyCommented:
The code below will do it. Note that you need to pass in the dimensions of the array, and you need a separator character that does not appear in your strings. I'm not sure why you are using a BinaryWriter instead of a textWriter.

When you come back later to read the file, you will have to know the number of rows and columns to reconstitute the array.

Jim

// Heap is size[rows,cols]
static void SaveHeap(string[,] heap, int rows, int cols)// called by Main
  {
   char separator = '\n';
      BinaryWriter bw = new BinaryWriter(new FileStream("HeapFile.txt", FileMode.OpenOrCreate));
 
      for (int i = 0; i < rows; i++)
      {
      for (int j = 0; j < cols; j++)
      {
         bw.Write(heap[i, j]);
         bw.Write(separator);
      }
      }
   bw.Flush();
   bw.Close();
 
      WriteToLog.WriteLine("*SaveHeap done - Head has __ nodes");
      WriteToLog.WriteLine();
  }

Open in new window

0
JimBrandleyCommented:
You could also write the Rows and Cols values at the start of the file. Then your reader could handle any dimensions.

Jim
0
JimBrandleyCommented:
After investigating some more, the separator is not needed. You can do it as below.

Jim

// Heap is size[rows,cols]
static void SaveHeap(string[,] heap, int rows, int cols)// called by Main
{
      BinaryWriter bw = new BinaryWriter(new FileStream("HeapFile.txt", FileMode.OpenOrCreate));
 
      bw.Write(rows);
      bw.Write(cols);
      for (int i = 0; i < rows; i++)
      {
         for (int j = 0; j < cols; j++)
         {
            bw.Write(heap[i, j]);
         }
      }
      bw.Flush();
      bw.Close();
 
      WriteToLog.WriteLine("*SaveHeap done - Head has __ nodes");
      WriteToLog.WriteLine();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.