473,322 Members | 1,232 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,322 software developers and data experts.

Binary file I/O using methods of streamreader and streamwriter

Hello,

I wonder if anyone could help me.

I'm using vb.NET and I'd like to read a binary file, byte by byte, and
then write to another file (making a duplicate, identical file).

I'd then like to modify the program to take the integer value of each
byte and say, add or subtract an integer from it, with the result
still being an integer from 0 to 255. Then write another file that is
just a list of the integers (as a column of numbers).

I think that vb6 allowed the use of GET and PUT, but I don't think
vb.NET allows those anymore. I believe I have to use the methods of
streamReader and streamWriter to accomplish the task, but I don't know
how.

Thanks,
Dave
Nov 20 '05 #1
4 17084
* pd******@yahoo.com (David Buchan) scripsit:
I wonder if anyone could help me.

I'm using vb.NET and I'd like to read a binary file, byte by byte, and
then write to another file (making a duplicate, identical file).

I'd then like to modify the program to take the integer value of each
byte and say, add or subtract an integer from it, with the result
still being an integer from 0 to 255. Then write another file that is
just a list of the integers (as a column of numbers).

I think that vb6 allowed the use of GET and PUT, but I don't think
vb.NET allows those anymore. I believe I have to use the methods of
In VB.NET, they are called 'FileGet' and 'FilePut'.
streamReader and streamWriter to accomplish the task, but I don't know
how.


Alreadly had a look at the 'Read', 'ReadLine', 'Write', 'WriteLine'
methods and their overloaded versions?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
David,
If you are operating strictly byte reads & writes all you need is Stream or
FileStream.

I would use something like:

Dim inputPath As String
Dim outputPath As String

Dim input As New FileStream(inputPath, FileMode.Open,
FileAccess.Read)
Dim output As New FileStream(outputPath, FileMode.Create,
FileAccess.Write)

Dim count As Integer = 1024
Dim buffer(count - 1) As Byte

count = input.Read(buffer, 0, count)
Do Until count = 0
' modify each byte in buffer here
output.Write(buffer, 0, count)
count = input.Read(buffer, 0, count)
Loop

input.Close()
output.Close()
StreamReader & StreamWriter are more useful when you want to translate a
text stream to/from "primitive" values, such as Integer, Double, and String.

BinaryReader & BinaryWriter are useful when you want to translate a binary
stream to/from "primitive" values, such as Integer, Double, and String.

Hope this helps
Jay

"David Buchan" <pd******@yahoo.com> wrote in message
news:d4**************************@posting.google.c om...
Hello,

I wonder if anyone could help me.

I'm using vb.NET and I'd like to read a binary file, byte by byte, and
then write to another file (making a duplicate, identical file).

I'd then like to modify the program to take the integer value of each
byte and say, add or subtract an integer from it, with the result
still being an integer from 0 to 255. Then write another file that is
just a list of the integers (as a column of numbers).

I think that vb6 allowed the use of GET and PUT, but I don't think
vb.NET allows those anymore. I believe I have to use the methods of
streamReader and streamWriter to accomplish the task, but I don't know
how.

Thanks,
Dave

Nov 20 '05 #3

If you want to copy a file you can use "File" class from System.IO
E.x) File.Copy("SourceFileName", "DestFileName")

Now, if you want to work with the bytes of a file you can use "FileStream"
class
from the same namespace.
E.x) Dim FS as new Filestream("FileName",Mode)
Dim Buffer() as byte

'Get the bytes from file to a byte array
redim Buffer(FS.Length-1)
FS.Read(Buffer,0,Buffer.Length)

'Do your stuff... :-)
For i as int32=0 to Buffer.Length-1
Buffer(i) = '...... Your code.
next

'Save the byte array and close the stream.
FS.Position=0
FS.Write(Buffer,0,Buffer.length)
FS.Close

I hope this works for you...
George

? "David Buchan" <pd******@yahoo.com> ?????? ??? ??????
news:d4**************************@posting.google.c om...
Hello,

I wonder if anyone could help me.

I'm using vb.NET and I'd like to read a binary file, byte by byte, and
then write to another file (making a duplicate, identical file).

I'd then like to modify the program to take the integer value of each
byte and say, add or subtract an integer from it, with the result
still being an integer from 0 to 255. Then write another file that is
just a list of the integers (as a column of numbers).

I think that vb6 allowed the use of GET and PUT, but I don't think
vb.NET allows those anymore. I believe I have to use the methods of
streamReader and streamWriter to accomplish the task, but I don't know
how.

Thanks,
Dave

Nov 20 '05 #4
Thanks guys.

FileStream looks like just the ticket.

I appreciate all the responses.

Dave
Nov 20 '05 #5

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

Similar topics

5
by: TomB | last post by:
Anyone know of an example/tutorial for encrypting a binary file? I'm able to encrypt/decrypt simple text files, but anything more complicated craps out. Thanks TomB
9
by: ALI-R | last post by:
Hi,, I have two questions : 1) Is it mandatory that config file of a desktop application must be App.config 2) Is it possible to update config file in your code?? thanks for your help. ALI
5
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------39<WBR>­0C0F3E0099" without the...
14
by: vbMark | last post by:
Greetings, This seems like it should be simple but I can't figure out how to do this. I just want to append binary file 2 on to the end of binary file 1. Sample code please? Thanks!
3
by: I_AM_DON_AND_YOU? | last post by:
In my program I am using the notepad file to read/write data. I don't want that someone should be able to delete/change the contents of that file by opening that file in Notepad or other editor. ...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
4
by: DazedAndConfused | last post by:
I encryted a serialized binary formatted object. Now I can't figure out how to deserialize it so that I can decrypt it. I used this code encrypt and write it out: Dim fe As New...
15
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with...
15
by: Zytan | last post by:
I have installed the Visual C# 2005 Code Snippets, and the snippets for file handling use StreamReader and StreamWriter, instead of System.IO.File.*. VB 2005 code snippets don't use these. Why...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.