FileStream questions

J

John

I am trying to familiarize myself with filestreams, but I have run
into the following issues. I am hoping that someone can shed
some light on these issues for me please.

I am using the following sample code to write (keeping it simple)

private void button1_Click(object sender, EventArgs e)
{
String DATFILE = Directory.GetCurrentDirectory() + "\\Test.dat";
if (File.Exists(DATFILE))
File.Delete(DATFILE);

FileStream fs = new FileStream(DATFILE, FileMode.CreateNew);
BinaryWriter br = new BinaryWriter(fs);

for(int x = 0; x < 10; x++)
{
// Write the data using the BinaryWriter
br.Write("Test: " + x);
br.Write("Number: " + x);
}

br.Close();
fs.Close();
}

And this to read.

private void button2_Click(object sender, EventArgs e)
{
String DATFILE = Directory.GetCurrentDirectory() + "\\Test.dat";
if (File.Exists(FILE_NAME))
{
FileStream fs = new FileStream(DATFILE, FileMode.Open,
FileAccess.Read);
BinaryReader binr = new BinaryReader(fs);

for (int x = 0; x < fs.Length; ++x)
{
listBox1.Items.Add(binr.ReadString());
listBox1.Items.Add(binr.ReadInt32());
}
}
}


Now my questions are.

1. How do you read the file from beginning to end, reading from the first
record until there is no more records?

* my reading code is not correct I already know that, that's why I am
asking.

2. When saving to the file, do you always have to replace the entire file,
or can
you just update the information whenever any single item has changed?

I still have to add delete, edit, and search to finish it off. But this is
just
a start for me.


Thanks

John
 
P

Peter Duniho

[...]
1. How do you read the file from beginning to end, reading from the first
record until there is no more records?

You're writing strings, so you need to use BinaryReader.ReadString().
Looking at the docs, it appears to me that the ReadString() method will
throw an exception once you've reached the end of the stream.
2. When saving to the file, do you always have to replace the entire
file,
or can you just update the information whenever any single item has
changed?

You do not have to replace the entire file. However, that's not to say
that it'd be easy to replace individual chunks of data within the file.
The file is still a stream of bytes, even if you wrap the FileStream that
opened it with a BinaryWriter. You can replace bytes, but there's no
facility to insert or delete bytes. So unless you're trying to replace a
section of the file with an identical length section, you need to rewrite
all of the data after that section, at a minimum, to adjust for the change
in size of the chunk you're modifying.

It's not the sort of thing you'd want to do over and over for large files.

Pete
 
J

John Rogers

Thanks for the tip Pete, I did a bit of searching and now I have it working.
My problem was not reading the bool value of what I was putting in,
so everything looked wrong when the values came out.

When using the same scenario using BinaryWriter and FileStream, do you
know how to click on a treenode and have many records for that node
display in a listview? I can't even begin to think of how to do that.

know how to get one record to display, but if you have maybe 20
records attached to one node, I just don't know.

John

Peter Duniho said:
[...]
1. How do you read the file from beginning to end, reading from the first
record until there is no more records?

You're writing strings, so you need to use BinaryReader.ReadString().
Looking at the docs, it appears to me that the ReadString() method will
throw an exception once you've reached the end of the stream.
2. When saving to the file, do you always have to replace the entire
file,
or can you just update the information whenever any single item has
changed?

You do not have to replace the entire file. However, that's not to say
that it'd be easy to replace individual chunks of data within the file.
The file is still a stream of bytes, even if you wrap the FileStream that
opened it with a BinaryWriter. You can replace bytes, but there's no
facility to insert or delete bytes. So unless you're trying to replace a
section of the file with an identical length section, you need to rewrite
all of the data after that section, at a minimum, to adjust for the change
in size of the chunk you're modifying.

It's not the sort of thing you'd want to do over and over for large files.

Pete
 
C

Chizl

John said:
I am trying to familiarize myself with filestreams, but I have run
into the following issues. I am hoping that someone can shed
some light on these issues for me please.

I am using the following sample code to write (keeping it simple)

private void button1_Click(object sender, EventArgs e)
{
String DATFILE = Directory.GetCurrentDirectory() +
"\\Test.dat";
if (File.Exists(DATFILE))
File.Delete(DATFILE);

FYI, I believe that Directory.GetCurrentDirectory() is working folder, not
application folder.

To get application folder you want to do this:
String szPath = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6);

The substring(6) is because there is a "file:\" in front of the path..
 
P

Peter Duniho

Thanks for the tip Pete, I did a bit of searching and now I have it
working.
My problem was not reading the bool value of what I was putting in,
so everything looked wrong when the values came out.

Yes, with a binary format you have to be very careful to read the data
exactly the same way as it was written.

I find text formats, like comma-separated or XML to be more forgiving.
They are usually not as compact or efficient, but unless those a really
important, it can be better to go with what's simpler and less error-prone
than to worry about file size and i/o speed.
When using the same scenario using BinaryWriter and FileStream, do you
know how to click on a treenode and have many records for that node
display in a listview? I can't even begin to think of how to do that.

File i/o has no inherent connection to a TreeView or any other user
interface element. So you would have to preserve some sort of mapping
between each node and its relation to the data in the file.

There are a variety of ways to do that: you can subclass TreeNode to
contain your own data, you could maintain a separate data structure that
parallels the tree view (in this case, you might make the tree view an
observer of that data structure so that it's always reflecting what's in
your main data structure), or you could just assign some custom class
reference to the TreeNode.Tag property.

The basic idea would be to set up your data structure so that when the
user clicks on a node, you can directly retrieve information from that
node that will tell you where in the file you need to read data.

Pete
 

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