473,324 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Why is streamreader not reading entire file

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)
  11.  
  12. Do While FileLengthCounter < FileLength
  13. sLine = objReader.ReadLine()
  14.  
  15. If Not sLine Is Nothing Then
  16. Dim StringSearch As Integer = 0
  17. StringSearch = InStr(sLine.ToString, sTextDate)
  18. if StringSearch > 0 Then
  19.  
  20. 'do stuff
  21.  
  22. End If
  23.  
  24. End If
  25. End If
  26. FileLengthCounter = FileLengthCounter + 1
  27. Loop
  28. objReader.Close()
Jul 2 '10 #1
1 3100
Aimee Bailey
197 Expert 100+
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
  2.  
  3. Public Class Form1
  4.  
  5.     Sub SearchFile(ByVal file As String,search As String)
  6.  
  7.         Using fs As New FileStream(file, FileMode.Open, _
  8.                                    FileAccess.Read)
  9.             Using sr As New StreamReader(fs)
  10.  
  11.                 Dim CurrentLine = sr.ReadLine()
  12.                 If Not IsNothing(CurrentLine) And _
  13.                     CurrentLine.Contains(search) Then
  14.  
  15.                     'do stuff
  16.  
  17.                 End If
  18.  
  19.             End Using
  20.         End Using
  21.  
  22.     End Sub
  23.  
  24. End Class
  25.  
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!

Aimee
Jul 10 '10 #2

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

Similar topics

4
by: somaboy mx | last post by:
hi, I'm on winXPpro / apache 1.2 / php 4.4.x I'm experimenting with writing and reading from textfiles via php. I can create a file with fopen, write to it, but I'm having trouble reading...
3
by: Jeremy | last post by:
I have a most aggravating problem. I don't understand what is causing readlines() not to read all the lines in the file. I have the following syntax: # some initial stuff XS =...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
4
by: Rvo | last post by:
Hi all, I have a routine that reads a binary file into a string, this worked fine all the time, untill recently. Now the reading of the file seems to reach the end of file too soon. At the end...
4
by: Nina | last post by:
Hi everyone, Do you know why the following code only read certain number of lines of text file, but not the entire file? Dim sr As StreamReader Dim str As String Dim al As ArrayList = New...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
21
by: EdUarDo | last post by:
Hi all, I'm not a newbie with C, but I don't use it since more than 5 years... I'm trying to read a text file which has doubles in it: 1.0 1.1 1.2 1.3 1.4 2.0 2.1 2.2 2.3 2.4 I'm doing...
1
by: John | last post by:
I have a process that reads a text file then uploads the data into a database table. The text file has 10 lines at the end of the file that are blank BUT it appears that the enter key or space bar...
8
by: Lonifasiko | last post by:
Hi, Using Process class I asynchronously launch an executable (black box executable) file from my Windows application. I mean asynchronously because I've got an EventHandler for "Exited" event....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.