473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(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

Mar 5 '08 #1
2 2053
On Wed, 05 Mar 2008 14:27:07 -0800, John <jo************@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.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
Mar 6 '08 #2
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" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Wed, 05 Mar 2008 14:27:07 -0800, John <jo************@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.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

Mar 6 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shawn | last post by:
Hi. I'm using a FileStream (instead of just the path to the xml file) to load an XmlDocument. I'm doing this because I need to be able to prevent other processes to update the file I'm working on....
9
by: Tom | last post by:
I am working with the this object as oppose to the StreamReader object becuase I need to access a file (to find the contents) while an external application is updating the file. When I was...
5
by: Chris Fink | last post by:
How do I load a string into a FileStream without going to disk? For example, string abc = "This is a string"; How do I load abc into a FileStream? FileStream input = new FileStream(.....);
0
by: majiofpersia | last post by:
Hi Everyone and thanks in advance, I am trying to create a webservices which will read a binary file and create another file based on that first one and sends back the new file to the client. ...
0
by: lh | last post by:
The following method only works when i give the ASP.net account full permissions on the directory. It doesn't work when i give the directory Modify, Read &Execute, List Folder Contents, Read, and...
9
by: ljlevend | last post by:
I have two questions related to FileStreams. 1. Is there any way to determine whether a file has the permissions that are required by a FileStream constructor? For example, given the following...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
9
by: Tim_Mac | last post by:
hi, i'm not sure if i have chosen the best approach, but it seemed quite good to me. i have a collection class, containing business objects. the collection class is static and remains in-memory...
6
by: rn5a | last post by:
What's the difference between the 'Stream' object & the 'FileStream' object? A file can be opened using the following code snippets: -------------------- 'create a File object & StreamReader...
2
by: =?Utf-8?B?bXVzb3NkZXY=?= | last post by:
Hi guys I'm trying to code up a file downloading system for our website (C#). My basic plan is that when a user clicks a file download, a page opens that shows something, and fires the chunked...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.