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. 2 1121
Kevin R <me@privacy.net> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
On Sun, 3 Jul 2005 17:47:20 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote: Kevin R <me@privacy.net> wrote: 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.
Thanks, Jon, that 'using' directive makes a bit more sense.
and good news about the streams too.
Kevin R. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gensek |
last post by:
(I don't know how to make the title more specific in a useful way,
sorry.)
http://www.geocities.com/fusionary_2000/PingGUI.zip
Requires wxPython.
The part that fails is the function...
|
by: Generic Usenet Account |
last post by:
Compile the following snippet of code and run it. If the program spits
out bat:bat instead of bat:zat, what would you say? Would you say that
the compiler has a problem, or would you lay the...
|
by: tom |
last post by:
Hallo,
I need help on MIDI file,
and precisely about retrieving
few note values
(mainly the "Velocity" value,
corrisponding to the intensity
of a played note)
while the MIDI is playing.
|
by: rodchar |
last post by:
hey all,
i noticed when i'm in the code-behind and i type, let's just say the word
IF, well in the auto-complete list the tool tip show it as a code-snippet. Is
there a key-stroke to stub the...
|
by: frk.won |
last post by:
I am interested in learning how to use the VS 2005 code snippets.
However, I wish to know what are the best ways to source control the
code snippets?
Are there any source safe/subversion...
|
by: Kyote |
last post by:
I used to use Code Snippet Editor back when I was working on VB. I
loved it. I ended up getting away from programming for a while. Now
that I'm starting to dabble again I'd like to use a snippet...
|
by: tshad |
last post by:
I was watching a video that used a code snippet to create a property and
when you type "prop" tab tab, it would create the private variable as well
as the property definitions with the private...
|
by: tshad |
last post by:
Is there a way to use code snippets to quickly add multiple properties?
If you do on snippet for property, tabbing just goes around the fields. An
escape will leave you where you last typed.
...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |