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

StreamReader/StreamWriter problem

P: n/a
I have a problem where some data is being eliminated.

The problem is that the data contains signed numeric fields (the low-order
byte of a negative number uses the first 4 bits as a sign and the last 4
bits as the low-order digit. This produces byte values higher than X'7F'. To
be more specific these values are hexadecimal X'B0' through X'B9'. It
appears the program is eliminating any byte values greater than X'7F', thus
shortening the record. I am using StreamReader and StreamWriter for the
I/O.

Is it a possibility that the data contained in the file is written in UTF-7
and the code needs to be adjusted to UTF-8??? Here is how I am opening the
input file:

Public Function ReadInFile() As Integer
Dim reader As StreamReader
Dim output As String = String.Empty

Try
reader = File.OpenText(inFileName)
output = String.Format("Input file {0} is opened successfully",
inFileName)
Logger.Log(output)
Dim line As String = reader.ReadLine()
While Not line Is Nothing
Dim record As CGIRecordIn = New CGIRecordIn(line)
inRecords.Add(record)
line = reader.ReadLine()
End While

Catch ex As Exception
output = String.Format("Failed reading {0}", inFileName)
Logger.Log(output)
Finally
reader.Close()

End Try
ReadInFile = inRecords.Count
output = String.Format("Read {0} valid records from input file",
ReadInFile)
Logger.Log(output)
End Function

Here is how I am opening the output file:

Public Sub WriteOutFile()
Dim recordOut As CGIRecordOut
Dim writer As StreamWriter
Dim output As String
Try
writer = File.CreateText(outFileName)
output = String.Format("Output file {0} is opened successfully
for writing", outFileName)
Logger.Log(output)

For Each recordOut In outRecords
recordOut.Build()
writer.WriteLine(recordOut.Rec)
Next

Catch ex As Exception
output = String.Format("Failed writing to {0}", outFileName)
Logger.Log(output)
Finally
writer.Close()
End Try

Logger.Log("Finished writing output file")
End Sub

I am not too clear as to the parameter that is needed to tell VB.NET that
the file is being opened as UTF-8 and written in the same format. Any help
would be greatly appreciated.

Thanks!

Nov 17 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Thelonious Monk" <th***************@hotmail.comwrote in message
news:ea**************@TK2MSFTNGP06.phx.gbl...

Might want to look into

' StreamReader(filename, encoding)
Dim reader As New StreamReader(inFilename, System.Text.Encoding.UTF8)

and

' StreamWriter(filename, append?, encoding)
Dim writer As New StreamWriter(outFilename, False,
System.Text.Encoding.UTF8)

That might do the trick. I apologize for being too darn tired to test right
now...
Nov 17 '06 #2

P: n/a
Thanks, Mike C#. Looks like it worked. Thanks again for your help.

"Mike C#" <xy*@xyz.comwrote in message
news:8F***************@newsfe08.lga...
>
"Thelonious Monk" <th***************@hotmail.comwrote in message
news:ea**************@TK2MSFTNGP06.phx.gbl...

Might want to look into

' StreamReader(filename, encoding)
Dim reader As New StreamReader(inFilename, System.Text.Encoding.UTF8)

and

' StreamWriter(filename, append?, encoding)
Dim writer As New StreamWriter(outFilename, False,
System.Text.Encoding.UTF8)

That might do the trick. I apologize for being too darn tired to test
right now...

Nov 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.