search line number from txt file

abhit_kumar

Member
Joined
Aug 13, 2008
Messages
19
Programming Experience
Beginner
Hello Frnd,

I get stuck in one of the programme.

when we r trying to serach line number in txt file, and if txt file having only single column data then it returns correct line number, but if in txt file there is more than one column data then it deoesnt work..

For example:- txt file contain following patter of data

111 anil 222
333 ajit 444
555 anish 666
777 ankit 888
999 ajay 999

And suppose if we have to search data 444, its ocurring on 2nd line, but this code returns incorrect line no.
Also look at last line, there is data 999(on 3rd column), but since this line also occuring 999 on first column, in that case its also not working...

Based upon your code and some my modication, i have trying to resolve this issue, but cant get proper solution for above issues.

My code is as follows:-



VB.NET:
Imports System
Imports System.IO

Public Class Form1
    Dim storeLineNo As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = ""
        Dim fs As FileStream
        fs = New FileStream(Path.Combine(Application.StartupPath, "\testdr.txt"), FileMode.Open, FileAccess.Read)
        Dim sr As StreamReader
        sr = New StreamReader(fs)
        Dim strLine As String = String.Empty
        Dim intLineCount As Integer = 1
        Dim LineNo As Integer = 0
        While (sr.Peek() > -1)
            LineNo = LineNo + 1
            strLine = Convert.ToString(sr.ReadLine())
            If (strLine.Equals(TextBox1.Text)) Then
                storeLineNo += LineNo & ","
            End If
        End While
        If (storeLineNo = String.Empty) Then
            Label1.Text = ""
        Else
            Label1.Text = storeLineNo.ToString()
        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Label1.Text = ""
    End Sub
End Class
 
It sounds like you're just trying to search the last column of data.

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click

        Dim sr As System.IO.StreamReader = New System.IO.StreamReader("C:\TestFile.txt")

        Dim Line() As String = sr.ReadToEnd.Split(Environment.NewLine)

        For i As Integer = 0 To Line.Length - 1
            If Line(i).Substring(Line(i).LastIndexOf(" ") + 1, TextBox1.Text.Length) = TextBox1.Text Then
                TextBox2.Text = "Line " & i + 1
                Exit For
            End If
        Next

    End Sub
 
Alternative (simpler)

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click

'read the file into an array
        Dim lines() as String = File.ReadAllLines(path)

'for each line
        For i As Integer = 0 To lines.Length - 1

'if the line contains the search term, after the first column
            If lines(i).Length > 3 AndAlso lines(i).IndexOf(TextBox1.Text, 3) > 0 Then
                MessageBox.Show("Found at line " & i + 1)

            End If
        Next

    End Sub
 
Thanks all of you...for the reply....
i have test the code of cjard, its really simpler and working fine..
now i want one changes in this code, i want to dynamically select the file from any where in the hard disk and then search the Line number from txt...

for example: if i select file test.txt the it will work and then if i select another file like test2.txt then it will go to that file and display the result..

please help me as soon as possible.

Thanks,
Abhi
 
Thanks cjard for the reply..

but can you please let me know, how to manage that msdn link code in my existing code, can you please give me suggestion for this..i have tried it but not seems to be working.....

I have two button, the first button is upload and second one is save button.

But when i click to upload button then it will select tx file frm any location, and after that when i click save button then it will search the line number from that uploaded txt file...

please help me........
 
I'm a little confused as to what you're asking. It sounds like you want to have an upload button where the user selects a file and then a save button where you search the file using the method Cjard gave you.

VB.NET:
    Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles btnUpload.Click

        Me.OpenFileDialog1.ShowDialog()

    End Sub

VB.NET:
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles btnSave.Click

        Dim lines() As String = File.ReadAllLines(Me.OpenFileDialog1.FileName())
        ...

    End Sub
 

Latest posts

Back
Top