Storing data of file in to string

J

Jason Roozee

The following doesnt work. The PeekChar either gives me an error
EndOfStreamException or gives me Index out of bounds error. All I am trying
to do is load the contents of the file in to a string. The file is a crystal
reports file. I am trying to take the file and store it in to a sql server
2000 database in to a "text" field. When I try to load the data in to a Char
array and then convert the array to a string -- the data doesnt match. Some
characters are converted to null, etc.

Any ideas whats wrong or how I should be doing this?

Dim fileDataStream As FileStream = New FileStream(ReportFile.Text,
FileMode.Open, FileAccess.Read, FileShare.None)

Dim fileDataBinaryReader As BinaryReader = New BinaryReader(fileDataStream)

fileDataBinaryReader.BaseStream.Seek(0, SeekOrigin.Begin)

Do Until fileDataBinaryReader.PeekChar = -1

FileData.Append(fileDataBinaryReader.ReadChar)

Loop
 
A

_Andy_

The following doesnt work. The PeekChar either gives me an error
EndOfStreamException or gives me Index out of bounds error. All I am trying
to do is load the contents of the file in to a string. The file is a crystal
reports file. I am trying to take the file and store it in to a sql server
2000 database in to a "text" field. When I try to load the data in to a Char
array and then convert the array to a string -- the data doesnt match. Some
characters are converted to null, etc.

Any ideas whats wrong or how I should be doing this?

Dim fileDataStream As FileStream = New FileStream(ReportFile.Text,
FileMode.Open, FileAccess.Read, FileShare.None)

Dim fileDataBinaryReader As BinaryReader = New BinaryReader(fileDataStream)

fileDataBinaryReader.BaseStream.Seek(0, SeekOrigin.Begin)

Do Until fileDataBinaryReader.PeekChar = -1

FileData.Append(fileDataBinaryReader.ReadChar)

Loop

Dim sr As System.IO.StreamReader =
System.IO.File.OpenText(theFileName)
Dim theTextFromTheFile As String = sr.ReadToEnd()
sr.Close()

(Do the whole thing inside a Try..Catch...End Try)

Rgds,
 
T

Tom Shelton

The following doesnt work. The PeekChar either gives me an error
EndOfStreamException or gives me Index out of bounds error. All I am trying
to do is load the contents of the file in to a string. The file is a crystal
reports file. I am trying to take the file and store it in to a sql server
2000 database in to a "text" field. When I try to load the data in to a Char
array and then convert the array to a string -- the data doesnt match. Some
characters are converted to null, etc.

Any ideas whats wrong or how I should be doing this?

Dim fileDataStream As FileStream = New FileStream(ReportFile.Text,
FileMode.Open, FileAccess.Read, FileShare.None)

Dim fileDataBinaryReader As BinaryReader = New BinaryReader(fileDataStream)

fileDataBinaryReader.BaseStream.Seek(0, SeekOrigin.Begin)

Do Until fileDataBinaryReader.PeekChar = -1

FileData.Append(fileDataBinaryReader.ReadChar)

Loop

Dim reader As StreamReader = File.OpenText("ReportFile.Text")
Dim fileData As String = reader.ReadToEnd()
reader.Close()

Console.WriteLine(fileData)

HTH
 
J

Jason Roozee

I tried that. And it doesnt work. The report file is not a TEXT file, its a
binary file. (Uses all 255 character set).

When I use either of the examples you guys provided me, the string length is
~68,000. But the file size in bytes is ~104,000.

It's a Crystal Report RPT file... Think of the file as a bmp file.


Thanks!
Jason Roozee
 
C

Cor

Hi Jason,

Did you try to change it a little bit and using instead that peek,
a test of the input byte for -1?

Cor
 
J

Jason Roozee

If I do this:
Dim sr As StreamReader = File.OpenText(ReportFile.Text)
Do Until sr.Peek = -1
Dim bufferChar(0) As Char
sr.Read(bufferChar, 0, bufferChar.Length)
FileData &= bufferChar(0).ToString
Loop
sr.Close()

Then FileData only ends up being ~68,000 long (should be ~104,000)



If I do this:
Dim sr As StreamReader = File.OpenText(ReportFile.Text)
Do Until sr.Peek = -1
FileData &= sr.Read
Loop
sr.Close()



Then FileData is almost the right length (~105,000), but the data is all
numbers.



If I do this:
Dim sr As StreamReader = File.OpenText(ReportFile.Text)
Do Until sr.Peek = -1
FileData &= chr(sr.Read)
Loop
sr.Close()

It errors out...

Grrrrr.... This was sooooo simple to do in VB6!!!:::

Dim fileData as string

Open fileName for binary as #1
fileData = string$(lof(1), 0)
get #1, , filedata
Close #1

FileData now would have the exact contents of the file. No matter what the
file type was. I miss VB6 :(



Jason Roozee
 
C

Cor

Hi Jason,

I have this now before my nose, this is standard input from the Dos console,
with the peek I got only the first line. I think you are smart enough to try
this in your situation.

Cor

\\\
Dim sr As IO.StreamReader = p.StandardOutput
Dim sb As New System.Text.StringBuilder("")
Dim input As Integer = sr.Read
Do Until input = -1
sb.Append(ChrW(input))
input = sr.Read
Loop
Me.TextBox1.Text = sb.ToString
////
 
J

Jason Roozee

I get the error "Index was outside the bounds of the array" on the line
"input = fileDataBinaryReader.Read". Why???


Dim fileDataStream As FileStream = New FileStream(ReportFile.Text,
FileMode.Open, FileAccess.Read, FileShare.None)
Dim fileDataBinaryReader As BinaryReader = New BinaryReader(fileDataStream)
Dim input As Integer = fileDataBinaryReader.Read
Do Until input = -1
FileData.Append(ChrW(input))
input = fileDataBinaryReader.Read
Loop
 

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

Top