By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,816 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,816 IT Pros & Developers. It's quick & easy.

Using BinaryWriter to write char[] array to file

P: n/a
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);
}
}
}
}
Jul 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Mark Miller <no**********@waveshift.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

P: n/a
Thanks for the quick reply John. That clears up a lot.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Mark Miller <no**********@waveshift.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3

P: n/a
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Mark Miller <no**********@waveshift.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.