Count number of lines in a text file is not working

SuperMario

Member
Joined
Mar 11, 2013
Messages
14
Location
The Netherlands, near Rotterdam
Programming Experience
Beginner
I have a simple task of counting number of lines in a text file, but i does not work.
but i dont see why...

VB.NET:
Dim myInleesStream As StreamReader

        Label1.Text = "Aantal regels: 0"

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            txtFilenaam.Text = OpenFileDialog1.FileName
            myInleesStream = New StreamReader(txtFilenaam.Text)

            While Not myInleesStream.ReadLine Is Nothing

                Counter += 1
            End While

End If
        Label1.Text = "Aantal regels:" + Counter.ToString

Can somebody help me??
 
Last edited:
If all you need is to count the number of lines, why do you even need a streamreader?

        Dim counter As Integer = 0
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            counter = File.ReadAllLines(OpenFileDialog1.FileName).Count
        End If
        Label1.Text = "Aantal regels:" & counter
 
I solved it, onnly for on a different way.

VB.NET:
  If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            txtFilenaam.Text = OpenFileDialog1.FileName
            myInleesStream = New StreamReader(txtFilenaam.Text)




            Do Until myInleesStream.ReadLine Is Nothing
                Counter = Counter + 1
            Loop


            Label1.Text = Counter.ToString
            myInleesStream.Close()


        End If


I used streamreader because of the following,From the file i just read, i want to create a new textfile With the first 5 and the last 5 lines of that file.
The first five is no problem so far, but he last 5...Dont know how to do it..
Do you maybe have a sample or a tutorial??
I quite new to VB.net

VB.NET:
 If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then           
     myWegschrijfStream = New StreamWriter(Path + "Test.txt")
                myInleesStream = New StreamReader(txtFilenaam.Text)




                For Regelindex = (AantalBeginCodes - (AantalBeginCodes - 1)) To AantalBeginCodes


                    Regel = myInleesStream.ReadLine
                    myWegschrijfStream.WriteLine(Regel)


                Next


  End If
            myInleesStream.Close()
        End If


        myWegschrijfStream.Close()


This is what i have so far...
This Writes the first five lines of the text file. It works fine...

But i dont know how i can get the last five lines of the file.
 
Last edited:
From the file i just read, i want to create a new textfile With the first 5 and the last 5 lines of that file.
The first five is no problem so far, but he last 5...Dont know how to do it..
Do you maybe have a sample or a tutorial??
        Dim lines = IO.File.ReadAllLines("input.txt")
        Dim out = lines.Where(Function(line, ix) ix < 5 Or ix > lines.Count - 6)
        IO.File.WriteAllLines("output.txt", out)
 
Since you already have the counter in your loop, just get the total number and subtract 5. Then reopen the file and restart the loop till you get to that number. If counter = total - 5, then copy that line to your new file. Decrement the total till you get to the last line.
 
Reading the file twice (once to count the lines and once to get the desired lines) would work but would be very poor. JohnH's solution is the simplest and what you should use if the files will always be relatively small. If the file is large though, reading the whole thing into memory in one go might be an issue. In that case you can read the file line by line and discard each line as you confirm that it's not required. To read a file line by line, I would tend to use File.ReadLines in .NET 4.0 and later or use a StreamReader in .NET 3.5 or later. Here's a solution that provides the output you want without caring how many lines there actually are, although you can count them as you read if you want:
Dim outputLines As New List(Of String)

For Each line In File.ReadLines(inputFilePath)
    outputLines.Add(line)

    If outputLines.Count > 10 Then
        outputLines.RemoveAt(5)
    End If
Next

File.WriteAllLines(outputFilePath, outputLines)
That will output the first five and last five lines of the file and it also handles a file with fewer than ten lines.
 
Back
Top