Read/Write Files

T

Tibby

I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file.

--
Thanks


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.538 / Virus Database: 333 - Release Date: 11/10/2003
 
A

Armin Zingler

Tibby said:
I need to read/write not only text files, but binary as well. It
seems like on binary files, it doesn't right the last 10% of the
file.

How do you read?
 
H

Herfried K. Wagner [MVP]

* "Tibby said:
I need to read/write not only text files, but binary as well.  It seems like on binary files, it doesn't right the last 10% of
the file.

Post your code.
 
T

Tibby

Here's the code:

Public Sub TestReadWrite(ByVal FileName As String)
Dim FS As FileStream
Dim BR As BinaryReader
Dim BW As BinaryWriter
Dim Storeage() As Byte
FS = New FileStream("C:\" & FileName, FileMode.Open, FileAccess.Read)
BR = New BinaryReader(FS)
Storeage = BR.ReadBytes(FS.Length)
BR.Close()
FS.Close()
FS = New FileStream("C:\Copy of " & FileName, FileMode.CreateNew,
FileAccess.Write)
BW = New BinaryWriter(FS)
BW.Write(Storeage)
BW.Close()
FS.Close()
End Sub

Don't ask me why or how, but now it seems to be working just fine. All I
can say is Grrrr....
The point is, I'm using it to replace filecopy/ftp functions. For the FTP,
there's a server app, then the client app connects in, and the data stream
is sent encrypted to the client. Once again, urgghhhh......
I think what I may have been doing is trying to read/write it as string
instead of byte. Hmmm...

Thanks again
Tibby
 
T

Tibby

<snip>

Hah, here we go! Try this code...

Public Sub TestReadWrite(ByVal FileName As String)
Dim FS As FileStream
Dim BR As BinaryReader
Dim BW As BinaryWriter
Dim Storeage() As Byte
Dim Play As String
Dim Encoder As New System.Text.ASCIIEncoding()
FS = New FileStream("C:\" & FileName, FileMode.Open, FileAccess.Read)
BR = New BinaryReader(FS)
Storeage = BR.ReadBytes(FS.Length)
Play = Encoder.GetString(Storeage)
BR.Close()
FS.Close()
FS = New FileStream("C:\Copy of " & FileName, FileMode.CreateNew,
FileAccess.Write)
BW = New BinaryWriter(FS)
Storeage = Encoder.GetBytes(Play)
BW.Write(Storeage)
BW.Close()
FS.Close()
End Sub

See, I need to use what I read in as a string datatype, then write it back
out. Something's being lost in the conversion. I'll look somemore, but
I'm pretty sure that I cannot use it as a byte array....

Thanks again
Tibby
 
J

Jay B. Harlow [MVP - Outlook]

Tibby,
See, I need to use what I read in as a string datatype, then write it back
out. Something's being lost in the conversion. I'll look somemore, but
I'm pretty sure that I cannot use it as a byte array....

Are you certain your file is ASCII Encoding and its not ANSI Encoding with a
specific code page or another encoding?

For information on Encoding, Unicode & character sets see:
http://www.joelonsoftware.com/articles/Unicode.html
&
http://www.yoda.arachsys.com/csharp/unicode.html



If you are making a copy of the file, why bother with Encoding at all??

In fact why bother with the BinaryReader or BinaryWriter??

I would use BinaryReader, BinaryWriter & Encoding objects when my program
needs to PROCESS the data, if I simply need an exact duplicate of the file I
would simply use Stream.Read & Stream.Write, which are inherited by
FileStream.

Something like:
Public Sub TestReadWrite(ByVal FileName As String)
Dim input As New FileStream(fileName, FileMode.Open,
FileAccess.Read)
Dim output As New FileStream("Copy of " & fileName,
FileMode.CreateNew, FileAccess.Write)

Dim count As Integer = 32 * 1024
Dim storage(count - 1) As Byte

count = input.Read(storage, 0, count)
Do Until count = 0
output.Write(storage, 0, count)
count = input.Read(storage, 0, count)
Loop
input.Close()
output.Close()

You can change count to be the size of buffer you want, just be mindful of
Memory Usage versus disk/network IO.

Hope this helps
Jay
 
T

Tibby

Here's the basics of the project... I need to open a file, whether it's
a text file, .exe, .pdf, etc., and take the contents, convert to string,
maybe can use char array, and then process that. The processing of it is
some kinda data re-arranging or something, not sure. I've been tasked with
just this part, then hand it off to the other team.

Thanks
Tibby
 
J

Jay B. Harlow [MVP - Outlook]

Tibby,
Oops, I misunderstood when you said "replace filecopy" as that suggested to
me you are doing a simple file copy.

As I stated "Something's being lost in the conversion" with string data is
normally caused by using the incorrect Encoding object.

Hope this helps
Jay


Tibby said:
Here's the basics of the project... I need to open a file, whether it's
a text file, .exe, .pdf, etc., and take the contents, convert to string,
maybe can use char array, and then process that. The processing of it is
some kinda data re-arranging or something, not sure. I've been tasked with
just this part, then hand it off to the other team.

Thanks
Tibby

with
<<snip>>
 
T

Tibby

It only does it with binaries such as applications or such, so what type of
encoding would that use? Hmm, I think I saw something in the StreamReader
class about getting the encoding used.....


Tibby
 
J

Jay B. Harlow [MVP - Outlook]

Tibby,
It only does it with binaries such as applications or such,
What only does what with binaries & such?

Your code only fails with binaries? Different binaries can have different
encodings. with NO indications on what encoding is used.

Whether you have a Text File or a Binary File you need to be certain to use
the current Encoding to convert binary Data into a String. Even if that
Encoding object is System.Text.UnicodeEncoding.
Hmm, I think I saw something in the StreamReader
class about getting the encoding used.....
Yes the StreamReader has CurrentEncoding object, remember however that the
default encoding for StreamReader is UTF8, not your Windows current ANSI
Code Page. You need to use System.Text.Encoding.Default to get the Encoding
object for your current ANSI Code Page in Windows. Which may or may not be
your problem.

Hope this helps
Jay
 
T

Tibby

Hmm, sounds pretty complex. Was wondering if there's a way to read a .exe
in pure string format like one could do in VB6? It worked fine there.....

Thanks
Tibby
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top