Using BinaryWriter to write char[] array to file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Miller

    #1

    Using BinaryWriter to write char[] array to file

    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 RandomFileAcces s
    {
    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.Nam e);
    bw.Flush();
    fs.Close();
    }

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

    if(len < CHAR_ARRAY_LENG TH){
    _name = name.PadRight(C HAR_ARRAY_LENGT H, ' ').ToCharArray( );
    }else if (len > CHAR_ARRAY_LENG TH){
    _name = name.Substring( 0, CHAR_ARRAY_LENG TH).ToCharArray ();
    }else{
    _name = name.ToCharArra y();
    }
    }
    get{
    return new string(_name);
    }
    }
    }
    }


  • Jon Skeet [C# MVP]

    #2
    Re: Using BinaryWriter to write char[] array to file

    Mark Miller <no_mark_spam@w aveshift.com> wrote:[color=blue]
    > 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?[/color]

    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.Unicod e.

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

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Mark Miller

      #3
      Re: Using BinaryWriter to write char[] array to file

      Thanks for the quick reply John. That clears up a lot.

      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.19f8bc 2b9ac43d4b9898a 5@msnews.micros oft.com...[color=blue]
      > Mark Miller <no_mark_spam@w aveshift.com> wrote:[color=green]
      > > I have a char[] array and when I write it to a file using BinaryWriter[/color][/color]
      the[color=blue][color=green]
      > > position of the pointer is the size of the array + 1. For example:[/color][/color]
      writing[color=blue][color=green]
      > > char[25] leaves the pointer at position 26 after starting at position 0.[/color][/color]
      I[color=blue][color=green]
      > > thought that char was 2 bytes, but this makes it seem as though it is[/color][/color]
      just 1[color=blue][color=green]
      > > when I write to a file. Why is this? I imagine the extra bit is just a[/color][/color]
      null[color=blue][color=green]
      > > bit (correct me if I'm wrong). I don't know if this helps but when I[/color][/color]
      fill[color=blue][color=green]
      > > the char[] array I use a padded string (to fill in empty elements) and[/color][/color]
      pass[color=blue][color=green]
      > > 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[/color][/color]
      and[color=blue][color=green]
      > > so I don't understand the behavior. Could it be the character encoding?[/color][/color]
      If[color=blue][color=green]
      > > so, how can I change the encoding, or determine the encoding of the[/color][/color]
      target[color=blue][color=green]
      > > machine?[/color]
      >
      > 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.Unicod e.
      >
      > One thing to note - in your sample code, you're not actually using
      > BinaryWriter.Wr ite(char[]) at all, you're using BinaryWriter.Wr ite
      > (string) which writes a length-prefixed string to the stream - that's
      > where the extra byte is coming from.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Mark Miller

        #4
        Re: Using BinaryWriter to write char[] array to file

        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 RandomFileAcces s
        {
        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.Unicod e); //<---------
        Using Unicode Encoding
        bw.Write(me.Nam e.ToCharArray() ); //<--------- Writing data as Char
        array
        bw.Flush();
        fs.Close();
        }

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

        if(len < CHAR_ARRAY_LENG TH){
        _name = name.PadRight(C HAR_ARRAY_LENGT H, ' ').ToCharArray( );
        }else if (len > CHAR_ARRAY_LENG TH){
        _name = name.Substring( 0, CHAR_ARRAY_LENG TH).ToCharArray ();
        }else{
        _name = name.ToCharArra y();
        }
        }
        get{
        return new string(_name);
        }
        }
        }
        }

        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
        news:MPG.19f8bc 2b9ac43d4b9898a 5@msnews.micros oft.com...[color=blue]
        > Mark Miller <no_mark_spam@w aveshift.com> wrote:[color=green]
        > > I have a char[] array and when I write it to a file using BinaryWriter[/color][/color]
        the[color=blue][color=green]
        > > position of the pointer is the size of the array + 1. For example:[/color][/color]
        writing[color=blue][color=green]
        > > char[25] leaves the pointer at position 26 after starting at position 0.[/color][/color]
        I[color=blue][color=green]
        > > thought that char was 2 bytes, but this makes it seem as though it is[/color][/color]
        just 1[color=blue][color=green]
        > > when I write to a file. Why is this? I imagine the extra bit is just a[/color][/color]
        null[color=blue][color=green]
        > > bit (correct me if I'm wrong). I don't know if this helps but when I[/color][/color]
        fill[color=blue][color=green]
        > > the char[] array I use a padded string (to fill in empty elements) and[/color][/color]
        pass[color=blue][color=green]
        > > 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[/color][/color]
        and[color=blue][color=green]
        > > so I don't understand the behavior. Could it be the character encoding?[/color][/color]
        If[color=blue][color=green]
        > > so, how can I change the encoding, or determine the encoding of the[/color][/color]
        target[color=blue][color=green]
        > > machine?[/color]
        >
        > 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.Unicod e.
        >
        > One thing to note - in your sample code, you're not actually using
        > BinaryWriter.Wr ite(char[]) at all, you're using BinaryWriter.Wr ite
        > (string) which writes a length-prefixed string to the stream - that's
        > where the extra byte is coming from.
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet
        > If replying to the group, please do not mail me too[/color]


        Comment

        Working...