Using BinaryWriter to write char[] array to file

  • Thread starter Thread starter Mark Miller
  • Start date Start date
M

Mark Miller

I have a char[] array and when I write it to a file using BinaryWriter the
position of the pointer is the size of the array + 1. For example: writing
char[25] leaves the pointer at position 26 after starting at position 0. I
thought that char was 2 bytes, but this makes it seem as though it is just 1
when I write to a file. Why is this? I imagine the extra bit is just a null
bit (correct me if I'm wrong). I don't know if this helps but when I fill
the char[] array I use a padded string (to fill in empty elements) and pass
it to char[] using the ToCharArray() method of the string class.

I am trying to create a file for random file access for the first time and
so I don't understand the behavior. Could it be the character encoding? If
so, how can I change the encoding, or determine the encoding of the target
machine?

Thanks in advance,
Mark

using System;
using System.IO;

namespace RandomFileAccess
{
class Test {
[STAThread]
static void Main(string[] args) {
myName me = new myName("Mark Miller");
FileStream fs = File.Create("DB.bin");
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(me.Name);
bw.Flush();
fs.Close();
}

}
class myName{
private const int CHAR_ARRAY_LENGTH = 15;
private char[] _name = new char[CHAR_ARRAY_LENGTH];
public myName(string sName){
Name = sName;
}
public string Name{
set{
string name = value;
int len = name.Length;

if(len < CHAR_ARRAY_LENGTH){
_name = name.PadRight(CHAR_ARRAY_LENGTH, ' ').ToCharArray();
}else if (len > CHAR_ARRAY_LENGTH){
_name = name.Substring(0, CHAR_ARRAY_LENGTH).ToCharArray();
}else{
_name = name.ToCharArray();
}
}
get{
return new string(_name);
}
}
}
}
 
Mark Miller said:
I have a char[] array and when I write it to a file using BinaryWriter the
position of the pointer is the size of the array + 1. For example: writing
char[25] leaves the pointer at position 26 after starting at position 0. I
thought that char was 2 bytes, but this makes it seem as though it is just 1
when I write to a file. Why is this? I imagine the extra bit is just a null
bit (correct me if I'm wrong). I don't know if this helps but when I fill
the char[] array I use a padded string (to fill in empty elements) and pass
it to char[] using the ToCharArray() method of the string class.

I am trying to create a file for random file access for the first time and
so I don't understand the behavior. Could it be the character encoding? If
so, how can I change the encoding, or determine the encoding of the target
machine?

Yes, it depends on the character encoding (and the characters written).
When you create the BinaryWriter you can set the encoding (there's a
constructor which takes an Encoding parameter).

If you want this file to be random access, you should probably pick a
fixed-size encoding, such as Encoding.Unicode.

One thing to note - in your sample code, you're not actually using
BinaryWriter.Write(char[]) at all, you're using BinaryWriter.Write
(string) which writes a length-prefixed string to the stream - that's
where the extra byte is coming from.
 
Thanks for the quick reply John. That clears up a lot.

Jon Skeet said:
Mark Miller said:
I have a char[] array and when I write it to a file using BinaryWriter the
position of the pointer is the size of the array + 1. For example: writing
char[25] leaves the pointer at position 26 after starting at position 0. I
thought that char was 2 bytes, but this makes it seem as though it is just 1
when I write to a file. Why is this? I imagine the extra bit is just a null
bit (correct me if I'm wrong). I don't know if this helps but when I fill
the char[] array I use a padded string (to fill in empty elements) and pass
it to char[] using the ToCharArray() method of the string class.

I am trying to create a file for random file access for the first time and
so I don't understand the behavior. Could it be the character encoding? If
so, how can I change the encoding, or determine the encoding of the target
machine?

Yes, it depends on the character encoding (and the characters written).
When you create the BinaryWriter you can set the encoding (there's a
constructor which takes an Encoding parameter).

If you want this file to be random access, you should probably pick a
fixed-size encoding, such as Encoding.Unicode.

One thing to note - in your sample code, you're not actually using
BinaryWriter.Write(char[]) at all, you're using BinaryWriter.Write
(string) which writes a length-prefixed string to the stream - that's
where the extra byte is coming from.
 
This is just a followup since whenever I'm looking for a solution on a ng I
like to see an example. Below the changes are indicated by the comments to
the right.

using System;
using System.IO;
using System.Text; //<--------- Added for Text Encoding Parameter

namespace RandomFileAccess
{
class Test {
[STAThread]
static void Main(string[] args) {
myName me = new myName("Mark Miller");
FileStream fs = File.Create("DB.bin");
BinaryWriter bw = new BinaryWriter(fs, Encoding.Unicode); //<---------
Using Unicode Encoding
bw.Write(me.Name.ToCharArray()); //<--------- Writing data as Char
array
bw.Flush();
fs.Close();
}

}
class myName{
private const int CHAR_ARRAY_LENGTH = 15;
private char[] _name = new char[CHAR_ARRAY_LENGTH];
public myName(string sName){
Name = sName;
}
public string Name{
set{
string name = value;
int len = name.Length;

if(len < CHAR_ARRAY_LENGTH){
_name = name.PadRight(CHAR_ARRAY_LENGTH, ' ').ToCharArray();
}else if (len > CHAR_ARRAY_LENGTH){
_name = name.Substring(0, CHAR_ARRAY_LENGTH).ToCharArray();
}else{
_name = name.ToCharArray();
}
}
get{
return new string(_name);
}
}
}
}

Jon Skeet said:
Mark Miller said:
I have a char[] array and when I write it to a file using BinaryWriter the
position of the pointer is the size of the array + 1. For example: writing
char[25] leaves the pointer at position 26 after starting at position 0. I
thought that char was 2 bytes, but this makes it seem as though it is just 1
when I write to a file. Why is this? I imagine the extra bit is just a null
bit (correct me if I'm wrong). I don't know if this helps but when I fill
the char[] array I use a padded string (to fill in empty elements) and pass
it to char[] using the ToCharArray() method of the string class.

I am trying to create a file for random file access for the first time and
so I don't understand the behavior. Could it be the character encoding? If
so, how can I change the encoding, or determine the encoding of the target
machine?

Yes, it depends on the character encoding (and the characters written).
When you create the BinaryWriter you can set the encoding (there's a
constructor which takes an Encoding parameter).

If you want this file to be random access, you should probably pick a
fixed-size encoding, such as Encoding.Unicode.

One thing to note - in your sample code, you're not actually using
BinaryWriter.Write(char[]) at all, you're using BinaryWriter.Write
(string) which writes a length-prefixed string to the stream - that's
where the extra byte is coming from.
 
Back
Top