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);
}
}
}
}
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);
}
}
}
}