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