0

Consider the following code below in which for each entry in key.txt I add it to each line in plain.txt and write it to the output.txt.

 Dim srKeyFile As New StreamReader("D:\Test\key.txt")

        Dim srOutFile As New StreamWriter("D:\TestBial\output.txt")
        Dim strKey, strPlain As String
        While srKeyFile.Peek() >= 0
            Dim srPlainFile As New StreamReader("D:\TestB\plain.txt")
            strKey = srKeyFile.ReadLine()
            Dim key As Integer = Integer.Parse(strKey)
            While srPlainFile.Peek() >= 0
                strPlain = srPlainFile.ReadLine()
                Dim plain As Integer = Integer.Parse(strPlain)
                srOutFile.WriteLine("" + (key + plain).ToString())
            End While
            srPlainFile.Close()
        End While
        srOutFile.Close()

Above I have to open and close the inner file on each iteration of the outer loop.Is there some way That I could position my pointer to beginning,each time I enter the inner loop for the file plain.txt

Naseer
  • 4,041
  • 9
  • 36
  • 72

1 Answers1

1

You can use StreamReader's BaseStream.Seek to do this, but you need to call DiscardBufferedData first:

    srPlainFile.DiscardBufferedData()
    srPlainFile.BaseStream.Seek(0, SeekOrigin.Begin)
xpda
  • 15,585
  • 8
  • 51
  • 82
  • why there is need of discardBuffer method? – Naseer Dec 12 '14 at 19:47
  • I'm not completely sure it's necessary, but it might be. Here's some info: http://msdn.microsoft.com/en-us/library/system.io.streamreader.discardbuffereddata%28v=vs.110%29.aspx – xpda Dec 12 '14 at 20:13