2016-07-23 11 views
-1

固定ファイルを経由してaを挿入するプログラムを作成しました。プログラムは正常に動作し、コンソールに正しく表示されます。問題は、ファイルの行に書き込むためにコンソールから行を取得できないことです。 すべての試みは、空のファイルで終わるか、個々の行として書かれた行の各文字列で終わります。次のコードは、出力をファイルに書き込む必要があるが、ファイルは空白のコードを示しています。.NET Console.WriteLine()Console.SetOut

Imports System.IO 
Module Module1 
    Sub Main() 

     Dim stdFormat As Integer() = {3, 13, 11, 5, 2, 2, 13, 14, 30, 15, 76, 80, 95, 100, 50, 2, 10, 30} 

     Using MyReader As New FileIO.TextFieldParser("SOURCE.txt") 
      MyReader.TextFieldType = FileIO.FieldType.FixedWidth 
      MyReader.FieldWidths = stdFormat 

      Dim currentRow As String() 
      While Not MyReader.EndOfData 

       Try 

        Dim rowType = MyReader.PeekChars(3) 

        If String.Compare(rowType, "Err") = 0 Then 


        Else 

         MyReader.SetFieldWidths(stdFormat) 

        End If 

        currentRow = MyReader.ReadFields 

        For Each newString In currentRow 


         Console.Write(newString & "|") 


        Next 


        Dim file = New FileStream("test.txt", FileMode.Append) 

        Dim standardOutput = Console.Out 

        Using writer = New StreamWriter(file) 

         Console.SetOut(writer) 

         Console.WriteLine() 

         Console.SetOut(standardOutput) 

        End Using 


       Catch ex As FileIO.MalformedLineException 



       End Try 

      End While 


     End Using 

     Console.ReadLine() 


    End Sub 

End Module 

答えて

0

はその後、その後、ストリームを標準をリセットし、(writerにリダイレクト)標準出力に改行を書き込み、writerにストリームを標準を設定しています。

あなたがする必要があるのは、ファイルに書き込むことです。ストリームをリダイレクトすることで邪魔しないでください。コンソールとファイルの両方の書き込みを組み合わせると、これを少しきれいにすることができます。

Using writer = New StreamWriter(file) 
    String newStr = "" 
    For Each columnStr In currentRow 
     newStr = columnStr & "|" 
     writer.WriteLine(newStr) 
     // If you don't want the console output, just remove the next line 
     Console.WriteLine(newStr) 
    Next 
End Using 
関連する問題