Not too hapy with this code snippet

K

Kevin R

This doesnt look right, though it appears to be working

BinaryWriter fileWriter;

for(i = 0; i < someNumber; i++)
{
fileWriter = new BinaryWriter(File.OpenWrite(filename));
// Write some stuff in to the file
// change the value of filename
fileWriter.Close();
}

Is it OK to be instantiating a new BinaryWriter object everytime the
loop is executed (possibly more than 16000 times)? or should I be
using some other method to open a different file each time.
Should I be destroying the BinaryWriter object at the bottom of the
loop each time ?

Another thing which is concerning me,
I have two streams reading from the same file

BinaryReader binReader = new BinaryReader(File.OpenRead(fileName));
BinaryReader dataReader = new BinaryReader(File.OpenRead(fileName));

can I use
dataReader.BaseStream.Position = someNumber;
to move dataReader's file position and be reasonably sure it will not
mess up binReader's file position ?

I've been using this quite successfully so far, so I am inclined to
think it may be OK, but as I am a complete newbie at C#, I thought
maybe I should ask some experts.

Kevin R.
 
J

Jon Skeet [C# MVP]

Kevin R said:
This doesnt look right, though it appears to be working

BinaryWriter fileWriter;

for(i = 0; i < someNumber; i++)
{
fileWriter = new BinaryWriter(File.OpenWrite(filename));
// Write some stuff in to the file
// change the value of filename
fileWriter.Close();
}

Is it OK to be instantiating a new BinaryWriter object everytime the
loop is executed (possibly more than 16000 times)? or should I be
using some other method to open a different file each time.

That's okay - but it's not the nicest code, IMO. I would use:

for (int i=0; i < someNumber; i++)
{
using (BinaryWriter fileWriter = new BinaryWriter (
File.OpenWrite(filename))
{
// Write some stuff in to the file
// change the value of filename
}
}

That way the file handle is closed whether or not an exception is
thrown.
Should I be destroying the BinaryWriter object at the bottom of the
loop each time ?

Well, you're closing it, which is the same as calling Dispose in this
case. That's as close to destroying the object as you actually get.
Another thing which is concerning me,
I have two streams reading from the same file

BinaryReader binReader = new BinaryReader(File.OpenRead(fileName));
BinaryReader dataReader = new BinaryReader(File.OpenRead(fileName));

can I use
dataReader.BaseStream.Position = someNumber;
to move dataReader's file position and be reasonably sure it will not
mess up binReader's file position ?

Yes - they are independent streams.
 
K

Kevin R

That's okay - but it's not the nicest code, IMO. I would use:

for (int i=0; i < someNumber; i++)
{
using (BinaryWriter fileWriter = new BinaryWriter (
File.OpenWrite(filename))
{
// Write some stuff in to the file
// change the value of filename
}
}

That way the file handle is closed whether or not an exception is
thrown.


Well, you're closing it, which is the same as calling Dispose in this
case. That's as close to destroying the object as you actually get.


Thanks, Jon, that 'using' directive makes a bit more sense.
and good news about the streams too.

Kevin R.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top