By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,435 Members | 2,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,435 IT Pros & Developers. It's quick & easy.

Read/Write Files

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
* "Tibby" <ti*************@hotmail.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
"Tibby" <ti*************@hotmail.com> schrieb
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?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3

P: n/a
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bp*************@ID-208219.news.uni-berlin.de...
* "Tibby" <ti*************@hotmail.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #4

P: n/a

"Tibby" <ti*************@hotmail.com> wrote in message

<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
Nov 20 '05 #5

P: n/a
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() End Sub
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
"Tibby" <ti*************@hotmail.com> wrote in message
news:Od**************@tk2msftngp13.phx.gbl...
"Tibby" <ti*************@hotmail.com> wrote in message

<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

Nov 20 '05 #6

P: n/a
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

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:#m**************@TK2MSFTNGP09.phx.gbl...
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()
End Sub


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
"Tibby" <ti*************@hotmail.com> wrote in message
news:Od**************@tk2msftngp13.phx.gbl...

"Tibby" <ti*************@hotmail.com> wrote in message

<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


Nov 20 '05 #7

P: n/a
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.
For information on Encoding, Unicode & character sets see:
http://www.joelonsoftware.com/articles/Unicode.html
&
http://www.yoda.arachsys.com/csharp/unicode.html

Hope this helps
Jay
"Tibby" <ti*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:#m**************@TK2MSFTNGP09.phx.gbl...
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

<<snip>>
Nov 20 '05 #8

P: n/a
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
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:eO**************@TK2MSFTNGP10.phx.gbl...
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.
For information on Encoding, Unicode & character sets see:
http://www.joelonsoftware.com/articles/Unicode.html
&
http://www.yoda.arachsys.com/csharp/unicode.html

Hope this helps
Jay
"Tibby" <ti*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in

message
news:#m**************@TK2MSFTNGP09.phx.gbl...
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

<<snip>>

Nov 20 '05 #9

P: n/a
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
"Tibby" <ti*************@hotmail.com> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl... 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
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:eO**************@TK2MSFTNGP10.phx.gbl...
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.
> For information on Encoding, Unicode & character sets see:
> http://www.joelonsoftware.com/articles/Unicode.html
> &
> http://www.yoda.arachsys.com/csharp/unicode.html
>


Hope this helps
Jay
"Tibby" <ti*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in

message
news:#m**************@TK2MSFTNGP09.phx.gbl...
> 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
>
>

<<snip>>


Nov 20 '05 #10

P: n/a
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

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:ep**************@TK2MSFTNGP09.phx.gbl...
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
"Tibby" <ti*************@hotmail.com> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
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
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in

message
news:eO**************@TK2MSFTNGP10.phx.gbl...
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.

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

Hope this helps
Jay
"Tibby" <ti*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
> 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
>
> "Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in
message
> news:#m**************@TK2MSFTNGP09.phx.gbl...
> > 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
> >
> >
<<snip>>



Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.