Results 1 to 6 of 6

Thread: .NET Txt File Line counter

  1. #1
    Registered+
    Join Date
    Aug 2005
    Location
    Liverpool/Brum Uni
    Posts
    18
    Thanks
    0
    Thanked
    0 times in 0 posts

    .NET Txt File Line counter

    Basicly at the moment I’ve got a file being read in, and then lines in the file being counted (this will eventually help build and array to hold the data).

    However at the moment it’s not counting the lines at all. I’m not sure what’s wrong with the code it looks fine to me. Hoping a fresh pair of eyes might point out what I’ve done wrong!
    Code:
    Public Class MicroMouse
        Inherits System.Windows.Forms.Form
    Private MMDATA As System.IO.StreamReader
    
        Dim MMFile As String
       
     Private Sub BtnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpenFile.Click
    
            OFDMicroData.Title = "Locate Micro Mouse Source File"
            OFDMicroData.Filter = "MicroMouse TAB Seperated File (*.txt)| *.txt"
    
            If (OFDMicroData.ShowDialog() = DialogResult.OK) Then
                MMFile = OFDMicroData.FileName
                MMDATA = System.IO.File.OpenText(MMFile)
                LBOpenedFile.Text = MMFile
    
            Else
                MsgBox("Please Select a valid MicroMouse Data File", MsgBoxStyle.Exclamation, "File Not Selected")
                LBOpenedFile.Text = "Error No File Selected"
            End If
            LineCounter()
        End Sub
        Private Sub LineCounter()
            Dim Curline, Letter As String
            Dim i, CharsInFile, LineCount As Short
            LineCount = 0
            CharsInFile = MMFile.Length
            For i = 0 To CharsInFile - 1
                Letter = MMFile.Substring(i, 1)
                If Letter = Chr(13) Then
                    LineCount += 1
                    i += 1
                End If
    
            Next i
            LBcount.Text = LineCount
        End Sub
    End Class

  2. #2
    Registered+
    Join Date
    Jan 2006
    Location
    London
    Posts
    28
    Thanks
    0
    Thanked
    0 times in 0 posts
    I'm not an expert when it comes to .NET - I'm learning myself..
    But I recently did something similar - you might find it easier to use the Readline method - if Readline returns Nothing, then you are at the end of the stream.

    For example:

    Code:
        Private Sub LineCounter()
            LineCount = 0
            Dim sLine As String = IOStreamReader.ReadLine
            While Not sPC Is Nothing
                 LineCount += 1
                 sLine = IOStreamReader.ReadLine
            End While
            LBcount.Text = LineCount
        End Sub
    Hope this is of some help!

  3. #3
    Sublime HEXUS.net
    Join Date
    Jul 2003
    Location
    The Void.. Floating
    Posts
    11,819
    Thanks
    213
    Thanked
    233 times in 160 posts
    • Stoo's system
      • Motherboard:
      • Mac Pro
      • CPU:
      • 2*Xeon 5450 @ 2.8GHz, 12MB Cache
      • Memory:
      • 32GB 1600MHz FBDIMM
      • Storage:
      • ~ 2.5TB + 4TB external array
      • Graphics card(s):
      • ATI Radeon HD 4870
      • Case:
      • Mac Pro
      • Operating System:
      • OS X 10.7
      • Monitor(s):
      • 24" Samsung 244T Black
      • Internet:
      • Zen Max Pro
    I spotted a post about this on another forum from a Microsoft Dev:

    Code:
    Dim LineCounter As Integer
    Dim LineText As String
    Dim MyStream As New System.IO.StreamReader("c:\test.txt")
    Do Until MyStream.Peek = -1
    	LineText = MyStream.ReadLine
    	LineCounter += 1
    Loop
    MessageBox.Show("This file contains " & LineCounter & " lines.")
    Might do the trick?
    (\__/)
    (='.'=)
    (")_(")

  4. #4
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    it should in theory be faster to just look for the \r or \n char. Because this way, a string has to be stacked each time, more memory is used (which has to be garbage collected etc.).

    My VB.net is terrible, so i'll not even try to write it, but i hope you can get the idea (if not i'll C# it up!)
    throw new ArgumentException (String, String, Exception)

  5. #5
    Sublime HEXUS.net
    Join Date
    Jul 2003
    Location
    The Void.. Floating
    Posts
    11,819
    Thanks
    213
    Thanked
    233 times in 160 posts
    • Stoo's system
      • Motherboard:
      • Mac Pro
      • CPU:
      • 2*Xeon 5450 @ 2.8GHz, 12MB Cache
      • Memory:
      • 32GB 1600MHz FBDIMM
      • Storage:
      • ~ 2.5TB + 4TB external array
      • Graphics card(s):
      • ATI Radeon HD 4870
      • Case:
      • Mac Pro
      • Operating System:
      • OS X 10.7
      • Monitor(s):
      • 24" Samsung 244T Black
      • Internet:
      • Zen Max Pro
    In classic it's just vbCRLF, I'm not that far into .NET yet..
    (\__/)
    (='.'=)
    (")_(")

  6. #6
    Registered+
    Join Date
    Aug 2005
    Location
    Liverpool/Brum Uni
    Posts
    18
    Thanks
    0
    Thanked
    0 times in 0 posts
    Quote Originally Posted by Stoo
    I spotted a post about this on another forum from a Microsoft Dev:

    Code:
    Dim LineCounter As Integer
    Dim LineText As String
    Dim MyStream As New System.IO.StreamReader("c:\test.txt")
    Do Until MyStream.Peek = -1
    	LineText = MyStream.ReadLine
    	LineCounter += 1
    Loop
    MessageBox.Show("This file contains " & LineCounter & " lines.")
    Might do the trick?
    Cheers! Worked a charm with a bit of editing. Thanks for all your responses guys!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help on my maxtor harddrives
    By arthurleung in forum PC Hardware and Components
    Replies: 10
    Last Post: 12-06-2007, 09:40 PM
  2. Max Rules
    By Trippledence in forum Networking and Broadband
    Replies: 0
    Last Post: 11-04-2006, 04:57 PM
  3. Nero vision express saying:'Burn process failed'
    By johnnr892 in forum Help! Quick Relief From Tech Headaches
    Replies: 15
    Last Post: 11-12-2005, 11:43 PM
  4. Nero or Burner ?
    By Foxile in forum Help! Quick Relief From Tech Headaches
    Replies: 30
    Last Post: 04-04-2005, 07:31 AM
  5. Dodgy DVD-r's ?
    By starside in forum Help! Quick Relief From Tech Headaches
    Replies: 12
    Last Post: 27-03-2005, 06:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •