473,467 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 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
3 5106
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peyman | last post by:
Hello all, This is my first c# program. And I seem to have a problem. Here is the code: FileStream myStream = new FileStream("d:\\test.txt", FileMode.Create, FileAccess.Write); BinaryWriter...
1
by: Marquee | last post by:
Hello, This is my first program c#, my background is c++. One thing I would like to do is put binary data (e.g. a record from disk, or network packet) in a struct. In C++ I would create the...
1
by: dsmith | last post by:
I need to write a data array out to disk, but unfortunately am running into performance issues. The function is essentially: private ushort theInt16Array = new ushort; public void...
3
by: Eugene | last post by:
I'm trying to write a class which uses BinaryWriter as its base but allows for queuing of write requests Public Class QueuedBinaryWriter Inherits BinaryWriter I override all the Write methods...
5
by: Travis Llewellyn | last post by:
I am writing a program that write out and array that I have stored. It writes out the whole array but adds a Carrot "^" in front of each section it writes. If I do a debug.writeline(hello) it...
3
by: Mark Miller | last post by:
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 leaves the pointer at position 26 after...
0
by: iwdu15 | last post by:
hi, im creating an instant messenger using TCP sockets, and everythings been fine until now. i want to be able to send rich text, but im hitting a wall here. what i want to do is send the font...
4
by: thiago777 | last post by:
I have only 1GB of RAM so I cannot work with files too big with the ReadtoEnd method. Here is the code Im trying so that the file would split in pieces of 256MB: Try Dim BInput...
8
by: =?Utf-8?B?Q2hyaXMgRmluaw==?= | last post by:
I am trying to make a minor modification to the code below and need some assistance. Currently this code is using the java.util, java.util.zip, and java.io assemblies from the vjslib.dll assembly....
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.