StreamReader/StreamWriter problem

T

Thelonious Monk

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!
 
M

Mike C#

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...
 

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