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

Why is streamreader not reading entire file

P: 1
Hello All,

I'm just getting into programming, bit of a newbie.

I'm having an issue with the streamreader. I had it working but I've changed something and it has stopped.

I have it in a Do While loop reading line by line, but it doesn't finish reading. It reads about 27 lines out of 4000, then stops.

I changed the contents of the file it was reading, but it's still just save as a txt notepad file.

I also changed how I tell it which file to open (UserSelection.T2) however it does open the file just doesn't finish reading it.

Doesn't kick back any errors, it just doesn't read the entire file.

Expand|Select|Wrap|Line Numbers
  1. Dim FSO As Object
  2. Dim File As Object
  3. FSO = CreateObject("Scripting.FileSystemObject")
  4. File = FSO.OpenTextFile(UserSelection.T2, 1)
  5. File.ReadAll()
  6. Dim FileLength As Integer
  7. FileLength = File.line - 1
  8. Dim FileLengthCounter As Integer = 0
  9. Dim sLine As String = ""
  10. Dim objReader As New StreamReader(UserSelection.T2)
  12. Do While FileLengthCounter < FileLength
  13. sLine = objReader.ReadLine()
  15. If Not sLine Is Nothing Then
  16. Dim StringSearch As Integer = 0
  17. StringSearch = InStr(sLine.ToString, sTextDate)
  18. if StringSearch > 0 Then
  20. 'do stuff
  22. End If
  24. End If
  25. End If
  26. FileLengthCounter = FileLengthCounter + 1
  27. Loop
  28. objReader.Close()
Jul 2 '10 #1
Share this Question
Share on Google+
1 Reply

Aimee Bailey
Expert 100+
P: 197
It's fantastic that your getting into programming, to point you in the right direction, here's an example of what i would do...

Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  3. Public Class Form1
  5.     Sub SearchFile(ByVal file As String,search As String)
  7.         Using fs As New FileStream(file, FileMode.Open, _
  8.                                    FileAccess.Read)
  9.             Using sr As New StreamReader(fs)
  11.                 Dim CurrentLine = sr.ReadLine()
  12.                 If Not IsNothing(CurrentLine) And _
  13.                     CurrentLine.Contains(search) Then
  15.                     'do stuff
  17.                 End If
  19.             End Using
  20.         End Using
  22.     End Sub
  24. End Class
Thanks to .Net's, there are many shorthand ways of doing things that save time. The example i've provided uses the FileStream and StreamReader directly, taking advantage of the Using keywords, we make sure that the file is closed and disposed once we have finished with it, and also using the String classes Contains function to make work much easier.

Ofcourse this is a slightly narrowed way of doing things, but hopefully from the example you will be able to expand on it.

Best Regards!

Jul 10 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.