Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

FileStream questions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John - 05 Mar 2008 22:27 GMT
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
Peter Duniho - 05 Mar 2008 23:59 GMT
> [...]
> 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
John Rogers - 06 Mar 2008 02:17 GMT
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

>> [...]
>> 1. How do you read the file from beginning to end, reading from the first
[quoted text clipped - 21 lines]
>
> Pete
Peter Duniho - 06 Mar 2008 02:39 GMT
> 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
Chizl - 06 Mar 2008 02:30 GMT
>I am trying to familiarize myself with filestreams, but I have run
> into the following issues.  I am hoping that someone can shed
[quoted text clipped - 8 lines]
>            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..
John Rogers - 06 Mar 2008 03:19 GMT
Cool, thanks.  I didn't pay any attention to that before.

> FYI, I believe that Directory.GetCurrentDirectory() is working folder, not
> application folder.
[quoted text clipped - 5 lines]
>
> The substring(6) is because there is a "file:\" in front of the path..

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.