StreamReader/StreamWriter problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thelonious Monk

    #1

    StreamReader/StreamWriter problem

    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(i nFileName)
    output = String.Format(" Input file {0} is opened successfully",
    inFileName)
    Logger.Log(outp ut)
    Dim line As String = reader.ReadLine ()
    While Not line Is Nothing
    Dim record As CGIRecordIn = New CGIRecordIn(lin e)
    inRecords.Add(r ecord)
    line = reader.ReadLine ()
    End While

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

    End Try
    ReadInFile = inRecords.Count
    output = String.Format(" Read {0} valid records from input file",
    ReadInFile)
    Logger.Log(outp ut)
    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(outp ut)

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

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

    Logger.Log("Fin ished 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!



  • Mike C#

    #2
    Re: StreamReader/StreamWriter problem


    "Thelonious Monk" <theloniousmonk god@hotmail.com wrote in message
    news:eaDB4lfCHH A.4312@TK2MSFTN GP06.phx.gbl...

    Might want to look into

    ' StreamReader(fi lename, encoding)
    Dim reader As New StreamReader(in Filename, System.Text.Enc oding.UTF8)

    and

    ' StreamWriter(fi lename, append?, encoding)
    Dim writer As New StreamWriter(ou tFilename, False,
    System.Text.Enc oding.UTF8)

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


    Comment

    • Thelonious Monk

      #3
      Re: StreamReader/StreamWriter problem

      Thanks, Mike C#. Looks like it worked. Thanks again for your help.

      "Mike C#" <xyz@xyz.comwro te in message
      news:8Fa7h.377$ Jm2.116@newsfe0 8.lga...
      >
      "Thelonious Monk" <theloniousmonk god@hotmail.com wrote in message
      news:eaDB4lfCHH A.4312@TK2MSFTN GP06.phx.gbl...
      >
      Might want to look into
      >
      ' StreamReader(fi lename, encoding)
      Dim reader As New StreamReader(in Filename, System.Text.Enc oding.UTF8)
      >
      and
      >
      ' StreamWriter(fi lename, append?, encoding)
      Dim writer As New StreamWriter(ou tFilename, False,
      System.Text.Enc oding.UTF8)
      >
      That might do the trick. I apologize for being too darn tired to test
      right now...
      >

      Comment

      Working...