Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Read line by line, compare a value and extract prtion to display

I have a text file on c, i need to open and read the first line, verify if the 5 characters are = to some other text, if yes, display the REST of the text in the line, then repeat for each line on the file.txt until it reads all the lines in the text file. not sure how to do it...

My "code"

VB
Dim fileReader As StreamReader fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\locatin1/filetoshow.txt")

    Dim stringReader As String
    stringReader = fileReader.ReadLine()
    ''if readline last end if not continue to next line....
    '' read first 6 characters and do if
    ' If stringReader = ""Dis1err" Then
    'Display1.Dis1RTB1.Text = "display the items to the right after character 6 to endofline"
    'Else
    ' If stringReader = ""Dis1jam" Then
    'Display1.Dis1RTB2.Text = "display the items to the right after character 6 to endofline"
    'Else
    ' If stringReader = ""Dis1???" Then
    'Display1.Dis1RTB3.Text = "display the items to the right after character 6 to endofline"
    'Else
    ' If stringReader = ""Dis2err" Then
    'Display2.Dis2RTB1.Text = "display the items to the right after character 6 to endofline"
    'Else
    ' If stringReader = ""Dis2jam" Then
    'Display2.Dis2RTB2.Text = "display the items to the right after character 6 to endofline"
    'Else
    ' If stringReader = ""Dis2???" Then
    'Display2.Dis1RTB3.Text = "display the items to the right after character 6 to endofline"
    'Else

    'stringReader.Close()


[UpDate]
thanks for the help I took your advise and this is how it looks

'Each type of Message sent
VB
Dim type1 As String = "d1err./"
Dim type2 As String = "d1jam./"
Dim type3 As String = "d1spe./"
Dim type4 As String = "d2err./"
Dim type5 As String = "d2jam./"
Dim type6 As String = "d2spe./"


VB
Dim fileReader As StreamReader = My.Computer.FileSystem.OpenTextFileReader("C:\\locatin1\\filetoshow.txt")
Dim stringReader As String = fileReader.ReadLine()

While stringReader <> Nothing
    Dim LineStart As String = stringReader.Substring(0, 7)
    Dim RestOfLine As String = stringReader.Substring(7)
    ' Perform your processing here...
    If LineStart = type1 Then
        Display1.Dis1RTB1.Clear()
        Display1.Dis1RTB1.Text = RestOfLine
    ElseIf LineStart = type2 Then
        Display1.Dis1RTB2.Clear()
        Display1.Dis1RTB2.Text = RestOfLine
    ElseIf LineStart = type3 Then
        Display1.Dis1RTB3.Clear()
        Display1.Dis1RTB3.Text = RestOfLine
    ElseIf LineStart = type4 Then
        Display2.Dis2RTB1.Clear()
        Display2.Dis2RTB1.Text = RestOfLine
    ElseIf LineStart = type5 Then
        Display2.Dis2RTB2.Clear()
        Display2.Dis2RTB2.Text = RestOfLine
    ElseIf LineStart = type6 Then
        Display2.Dis2RTB3.Clear()
        Display2.Dis2RTB3.Text = RestOfLine
    End If
    stringReader = fileReader.ReadLine()
End While
fileReader.Close()


Last 2 questions
1. will this read each line and apply the rules for each line?

example of the text file i open and read below:
<line1> d1err./ error on machine
<line2> d1jam./ jam on machine
<line3>d1spe./
<line4>d2err./
<line5>d2jam./ jam on machine
<line6>d2spe./ machine needs maintenance

much appreciated
Posted
Updated 23-Apr-16 15:13pm
v4
Comments
Sergey Alexandrovich Kryukov 23-Apr-16 14:05pm    
What is "VB"? :-)
—SA
Patrice T 23-Apr-16 14:54pm    
And you have a question ?

Your solution may look like this:
VB.NET
Dim fileReader As StreamReader = My.Computer.FileSystem.OpenTextFileReader("C:\\locatin1\\filetoshow.txt")
Dim stringReader As String = fileReader.ReadLine()
While stringReader <> Nothing
    Dim LineStart As String = stringReader.Substring(0, 6)
    Dim RestOfLine As String = stringReader.Substring(6)
    ' Perform your processing here...
    If LineStart = "Dis1err" Then
        Display1.Dis1RTB1.Text = RestOfLine
    ElseIf LineStart = "Dis1jam" Then
        Display1.Dis1RTB2.Text = RestOfLine
    End If
    stringReader = fileReader.ReadLine()
End While
fileReader.Close()

LineStart variable holds first 6 characters of the line. RestOfLine variable stores the rest of the line. Use these variables to build your displaying logic.
 
Share this answer
 
v2
Comments
Hacko123 23-Apr-16 17:10pm    
thanks for the help I took your advise and this is how it looks

'Each type of Message sent
Dim type1 As String = "d1err./"
Dim type2 As String = "d1jam./"
Dim type3 As String = "d1spe./"
Dim type4 As String = "d2err./"
Dim type5 As String = "d2jam./"
Dim type6 As String = "d2spe./"

<pre>
Dim fileReader As StreamReader = My.Computer.FileSystem.OpenTextFileReader("C:\\locatin1\\filetoshow.txt")
Dim stringReader As String = fileReader.ReadLine()

While stringReader <> Nothing
Dim LineStart As String = stringReader.Substring(0, 7)
Dim RestOfLine As String = stringReader.Substring(7)
' Perform your processing here...
If LineStart = type1 Then
Display1.Dis1RTB1.Clear()
Display1.Dis1RTB1.Text = RestOfLine
ElseIf LineStart = type2 Then
Display1.Dis1RTB2.Clear()
Display1.Dis1RTB2.Text = RestOfLine
ElseIf LineStart = type3 Then
Display1.Dis1RTB3.Clear()
Display1.Dis1RTB3.Text = RestOfLine
ElseIf LineStart = type4 Then
Display2.Dis2RTB1.Clear()
Display2.Dis2RTB1.Text = RestOfLine
ElseIf LineStart = type5 Then
Display2.Dis2RTB2.Clear()
Display2.Dis2RTB2.Text = RestOfLine
ElseIf LineStart = type6 Then
Display2.Dis2RTB3.Clear()
Display2.Dis2RTB3.Text = RestOfLine
End If
stringReader = fileReader.ReadLine()
End While
fileReader.Close()

Last 2 questions
1. will this read each line and apply the rules for each line?

example of the text file i open and read below:
<line1> d1err./ error on machine
<line2> d1jam./ jam on machine
<line3>d1spe./
<line4>d2err./
<line5>d2jam./ jam on machine
<line6>d2spe./ machine needs maintenance

much appreciated
Hacko123 23-Apr-16 20:42pm    
how to make it read and apply this for each line and apply the rules for each line?

example of the text file i open and read below:
<line1> d1err./ error on machine
<line2> d1jam./ jam on machine
<line3>d1spe./
<line4>d2err./
<line5>d2jam./ jam on machine
<line6>d2spe./ machine needs maintenance

much appreciated
Maksman 25-Apr-16 11:02am    
The code in the While loop is executed once for each line of the file. So your rules are already applied to each line.
I think you are using VB.Net ...

When I read the Code (from which most of the lines are not active) my first opinion is :
- You read the first line into the String-Variable 'StringReader'
- now you compare this Variable with some other String like for example "Dis1err"
- this would work if "Dis1err" is the only content of the line - but I suppose that the line has more content than this partial String. In this case you should look, if the Variable StringReader.CONTAINS("Dis1err") or maybe if it begins StringReader.SubString(0,6) with it. I think, that could solve your issue ...

At the End of all you should close the FileReader (and not the StringReader)
 
Share this answer
 
v2
Comments
Hacko123 23-Apr-16 17:08pm    
thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900