FileStream questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    FileStream questions

    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



  • Peter Duniho

    #2
    Re: FileStream questions

    On Wed, 05 Mar 2008 14:27:07 -0800, John <johnrogers2345 @aol.comwrote:
    [...]
    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.Re adString().
    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

    Comment

    • John Rogers

      #3
      Re: FileStream questions

      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" <NpOeStPeAdM@nn owslpianmk.comw rote in message
      news:op.t7kh91n 78jd0ej@petes-computer.local. ..
      On Wed, 05 Mar 2008 14:27:07 -0800, John <johnrogers2345 @aol.comwrote:
      >
      >[...]
      >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.Re adString().
      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

      Comment

      Working...