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(o bject sender, EventArgs e)
{
String DATFILE = Directory.GetCu rrentDirectory( ) + "\\Test.dat ";
if (File.Exists(DA TFILE))
File.Delete(DAT FILE);
FileStream fs = new FileStream(DATF ILE, FileMode.Create New);
BinaryWriter br = new BinaryWriter(fs );
for(int x = 0; x < 10; x++)
{
// Write the data using the BinaryWriter
br.Write("Test: " + x);
br.Write("Numbe r: " + x);
}
br.Close();
fs.Close();
}
And this to read.
private void button2_Click(o bject sender, EventArgs e)
{
String DATFILE = Directory.GetCu rrentDirectory( ) + "\\Test.dat ";
if (File.Exists(FI LE_NAME))
{
FileStream fs = new FileStream(DATF ILE, FileMode.Open,
FileAccess.Read );
BinaryReader binr = new BinaryReader(fs );
for (int x = 0; x < fs.Length; ++x)
{
listBox1.Items. Add(binr.ReadSt ring());
listBox1.Items. Add(binr.ReadIn t32());
}
}
}
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
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(o bject sender, EventArgs e)
{
String DATFILE = Directory.GetCu rrentDirectory( ) + "\\Test.dat ";
if (File.Exists(DA TFILE))
File.Delete(DAT FILE);
FileStream fs = new FileStream(DATF ILE, FileMode.Create New);
BinaryWriter br = new BinaryWriter(fs );
for(int x = 0; x < 10; x++)
{
// Write the data using the BinaryWriter
br.Write("Test: " + x);
br.Write("Numbe r: " + x);
}
br.Close();
fs.Close();
}
And this to read.
private void button2_Click(o bject sender, EventArgs e)
{
String DATFILE = Directory.GetCu rrentDirectory( ) + "\\Test.dat ";
if (File.Exists(FI LE_NAME))
{
FileStream fs = new FileStream(DATF ILE, FileMode.Open,
FileAccess.Read );
BinaryReader binr = new BinaryReader(fs );
for (int x = 0; x < fs.Length; ++x)
{
listBox1.Items. Add(binr.ReadSt ring());
listBox1.Items. Add(binr.ReadIn t32());
}
}
}
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
Comment