-2

I have two arrays from my own created zip program, farArray and bytes. They are both byte arrays.

Now I want to save that in a file (example: "file.zip"). I know that I can write bytes with this code:

File.WriteAllBytes(savefile.FileName, bytes);

But I only save one byteArray now. How do I save two of them?

And can I get back the two arrays if I open the file in my script?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I forgot to add this, I don't want to use methods. just code. – Pieter Berkel Jan 19 '16 at 15:14
  • Concatenate them together. Of course, now, unless your arrays are known, fixed lengths, you'll need to include some kind of header at the beginning that tells you where one array starts and the other ends. Or you need some kind of delimiter byte / sequence of bytes to show where the first one ends. – Matt Burland Jan 19 '16 at 15:16
  • 1
    *I don't want to use methods. just code*, what's that supposed to mean? – Matt Burland Jan 19 '16 at 15:16
  • example: copy.array(), I dont want that, I want to write the copy function on my own. – Pieter Berkel Jan 19 '16 at 15:17
  • *I want to write the copy function on my own* why? The `Copy` function works and is extensively tested by millions of programmers. Why would you reinvent (a potentially broken) wheel? – Matt Burland Jan 19 '16 at 15:18
  • I'm a beginner, I want to learn everything and I want to be consistent – Pieter Berkel Jan 19 '16 at 15:19
  • Then learn to use the tools that the library provides. – Matt Burland Jan 19 '16 at 15:20
  • You want to learn everything? Why do you want code from SO in this case? Create your own algorithm to solve this issue. – Andre Jan 19 '16 at 15:24
  • 1
    So, you'll need to add more information to the file than just the contents of the two byte arrays (e.g. the lengths of the arrays). Trying to come up with a way to do this yourself is a colossal waste of time. If the arrays are small, then serializing to JSON might be the best way to go, otherwise [binary serialization](https://msdn.microsoft.com/en-us/library/72hyey7b%28v=vs.110%29.aspx) would be more appropriate. I really wouldn't recommend making your own, new file format for storing this data. Alternatively, save two files. – spender Jan 19 '16 at 15:28
  • @spender: Unless the arrays happen to be fixed length, but otherwise, I completely agree. And even with fixed length arrays, I wouldn't bother to roll my own serialization without a *really* good reason. – Matt Burland Jan 19 '16 at 15:42

3 Answers3

2

Merge the two arrays and then perform a write operation:

byte[] array1; // This will be the array1
byte[] array2; // This will be the array1
byte[] combinedArray = new byte[array1.Length + array2.Length ];
Array.Copy(array1, 0, combinedArray, 0, array1.Length);
Array.Copy(array2, 0, combinedArray, array1.Length, array2.Length);

Now combinedArray is a byte array that contains all bytes from the array1 and array2. So writing this array will give you the result:

File.WriteAllBytes(savefile.FileName, combinedArray);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
1

If I understand you right:

using (var stream = new FileStream(savefile.FileName, FileMode.Append))
{
  stream.Write(bytes, 0, bytes.Length);
  stream.Write(farArray, 0, farArray.Length);
}

If you want to get back your arrays, you can use structered file format. JSON for example. Or set some label between arrays. Or write size of arrays in the head of file, and then use this data to get arrays back.

fryday
  • 387
  • 2
  • 14
1

So you want to write two arrays to a file. You can either concatenate your two arrays into a single array and write that (similar to un-lucky's answer) or you can write the arrays in two separate operations (like fryday's answer). Both of those are perfectly valid ways to do it. Although combining the arrays just for the purpose of writing it to a file is not as efficient, obviously.

Your bigger problem is what you are going to do when you want to read back that data. When you go to read it back, it's just going to look like one big array. How do you know where one array ends and the other starts?

There are a couple of ways you can think about handling that. If the arrays have a know, fixed size, then it's not that big of a deal. For example, if you know bytes is always 100 bytes long and farArray is always 200 bytes long (for example) then you can read it like this:

 var bytes = new byte[100];
 var farArray = new byte[200];
 stream.Read(bytes, 0, 100);
 stream.Read(farArray, 0, 200);

Easy. Now if your arrays aren't fixed length. Then probably the easiest solution would be to include a header at the start of the file that will contain the lengths of the two arrays. So when you write, you'd do something like:

 stream.Write(BitConverter.GetBytes(bytes.Length), 0, 4);
 stream.Write(BitConverter.GetBytes(farArray.Length), 0, 4);
 stream.Write(bytes, 0, bytes.Length);
 stream.Write(farArray, 0, farArray.Length);

Now the first 8 bytes (two Int32's) of your file will contain the lengths of the two arrays that follow. So to read you'd do something like:

 var byteLengthArray = new byte[4];
 var farLengthArray = new byte[4];
 stream.Read(byteLengthArray, 0, 4);
 stream.Read(farLengthArray, 0, 4);
 // Note: you may have to worry about endianness if a file is written 
 // with one architecture and read with another.
 var byteLength = BitConverter.ToInt32(byteLengthArray, 0);
 var farLength = BitConverter.ToInt32(farLengthArray, 0);
 // now we know how big the two arrays are!
 var bytes = new byte[byteLength];
 var farArray = new byte[farLength];
 stream.Read(bytes, 0, byteLength);
 stream.Read(farArray, 0, farLength);

Another way you could do it, although it would make reading even more fiddly, is to include some byte or group of bytes as a delimiter in between the two arrays. To do this you must be sure that the delimiter cannot occur anywhere else in either of the arrays. So you'd write something like:

 stream.Write(bytes, 0, bytes.Length);
 stream.Write(delimiter, 0, delimiter.Length);
 stream.Write(farArray, 0, farArray.Length);

Now reading this is a bit tricky. Probably the easiest way would be to read all the bytes, and then search for the delimiter string. Once you find the place (index) where the delimiter starts, you would copy all the bytes before it into bytes and all the bytes after (taking into account the length of the delimiter array) into farArray.

Of course, the easier way to avoid all this tricky and error-prone byte copying is to just use the serialization and deserialization tools provided by the framework. Unless you have a very good reason (like you need your files to be ultra-compact for example), you probably shouldn't invent your own file formats.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • It crashes at this line: stream.Read(byteLengthArray, 0, 4); – Pieter Berkel Jan 19 '16 at 15:47
  • 1
    @PieterBerkel: "It crashes" isn't a problem description. What error is it giving you? What does your code look like? – Matt Burland Jan 19 '16 at 15:50
  • I save the file with your second piece of save code. and then I open it with this: using (var stream = new FileStream(file_path, FileMode.Append)) { Here I work with your code }, And I get an unknown error. – Pieter Berkel Jan 19 '16 at 15:53
  • @PieterBerkel: See [FileMode](https://msdn.microsoft.com/en-us/library/system.io.filemode(v=vs.110).aspx) and the problem should be obvious. And again, include *what* error you get, I'm guessing a `NotSupportedException`? – Matt Burland Jan 19 '16 at 15:55
  • Thanks for you help, But now the code works if I use FileMode.Open. But I dont get the same output as the 2 arrays I saved. – Pieter Berkel Jan 19 '16 at 15:59
  • 1
    @PieterBerkel: Do some debugging. Set a break point and see what `byteLength` and `farLength` actually are and see if they match what you expect. – Matt Burland Jan 19 '16 at 16:03