I'm pretty stumped. I need to read a textfile using Streamreader in VB but not reall every single line
I have to look for the line that has a specific character in a specific position to indicate that this is a start point, then read that line and the remaining lines after until it hits the line with a specific character as a terminator. Then find the next set of lines that match until there are no more lines to read...? Can anyone help? I thought I had it but for some reason the data uploading to the DB wasn't correct :(

(sorry I originally posted this in the wrong area)

Hi,

Sorry but you will have to read in every line of the text in order to identify where the text you are looking for begins and ends and begins again if you see what I mean. You will only want to retain the sections of text you need. Why don't you post up what code you have done so far? Here is a simple example I have used in the past to identify tags in HTML:

sub Count_Tags( byref InputHTML as String)
dim ACount, BCount, ICount PCount as integer
dim i as integer
dim sChar as string
dim bInTag as Boolean 
dim sTag as string
for i =1 to len(InputHTML)
    sChar = mid(InputHTML,i,1)
    if bInTag then
        sTag &= lcase(sChar)
        if sChar =">" then
            if instr(sTag,"<a") <> 0 then
                ACount +=1
            elseif instr(sTag, <"b") <> 0 ANDALSO instr(sTag,"<br") =0 then
                BCount +=1
            elseIf instr(sTag,"<i") <> 0 ANDALSO instr(sTag, "<iframe") = 0 then
                ICount +=1
            elseif instr(stag, "<p") <> 0 then
                PCount +=1
            end if
            bInTag = false      
        end if
    elseif sChar ="<" then
        bInTag = true
        sTag="<"
    end if
next
Msgbox("There were " &PCount &" paragraphs, " &ACount &" web links, " &BCount &" instances of Bold text, " &ICount &" instances of Italic text.")

Supposed to read if the line has "(" in the 7th position and end if it sees "~~" then goes to the next matching set until end of text file. This is what I had (brain=fried):

Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        '  Open the appropriate file (2014HamTechnician, 2015HamGeneral or 2016HamExtra) and loop through the text file processing each line.  

        'Variable
        Dim db As New dbUpdt
        Dim strQuesNumLine As String = String.Empty    ' for each question Number line
        Dim strQuestion As String = String.Empty 'Question
        Dim strAnsA As String = String.Empty  'A Answer
        Dim strAnsB As String = String.Empty 'B Answer
        Dim strAnsC As String = String.Empty 'C Answer
        Dim strAnsD As String = String.Empty  'D Answer
        Dim strterm As String = String.Empty 'Question terminator
        ' Dim aryFields() As String ' array to contain fields
        Dim InputFile As New StreamReader("2014HamTechnician.txt") 'Open text file
        Dim strQuesKey As String 'Eight character of the Question Number line; answer

        'create connection to the database
        db.openConnection(strDB)


        ' continue processing until I hit the end of txt file

        Dim line As String
        ' continue processing until I hit the end of text file

        Do Until InputFile.EndOfStream
            line = InputFile.ReadLine


            If line.IndexOf("(") = 6 Then


            strQuesNumLine = line 'Extract Question Number Line
           strQuesKey = strQuesNumLine.Substring(7, 1) 'Eight character of the Question Number line; answer


            'add each record of QuesNum to the DB using the key
            db.AddRecord(strTable, "QuesKey", strQuesKey)
            'MessageBox.Show(strQuesKey)

            Dim strQuesNum As String = strQuesNumLine.Substring(3, 2) 'First five characters of the Question Number line
            db.setField(strTable, "QuesKey", strQuesKey, "QuesNum", strQuesNum)
            'MessageBox.Show(strQuesNum)


            Dim strQuesGroup As String = strQuesNumLine.Substring(0, 3) 'First three characters of the QuesNum
            db.setField(strTable, "QuesKey", strQuesKey, "QuesGroup", strQuesGroup)
            'MessageBox.Show(strQuesGroup)


                Dim strQuesRef As String = line.ToString 'Everything after the 10 character in the Question Number line
                db.setField(strTable, "QuesKey", strQuesKey, "QuesRef", strQuesRef)



            End If

        Loop

Here's another suggestion

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Dim inblock As Boolean = False

    For Each line As String In System.IO.File.ReadAllLines(filename)

        If inblock Then
            If line.Contains("~~") Then
                inblock = False
            Else
                ProcessLine(line)
            End If
        Else
            If line.Substring(7, 1) = "(" Then
                inblock = True
                ProcessLine(line)
            End If
        End If

    Next

End Sub

Using ReadAllLines makes the coding simpler. A text file would have to be very large before you have to worry about reading it line by line instead of using ReadAllLines. Note that I have moved the processing of the actual line to a separate Sub. I wasn't sure if the file containing the Start token would also contain a line to process. If not then remove the second call to ProcessLine.

Thanks, I'll give it a try!

This definitely worked as far as being able to read the lines properly. However not sure how to be able to separate each line into the different fields. For ex. first line it reads is field for Question Number field and the next line is the Question Text field etc...
I was able to split them by assigning each with Input.Readlines...

 strQuesText = InputFile.ReadLine
                strAnsA = InputFile.ReadLine
                strAnsB = InputFile.ReadLine
                strAnsC = InputFile.ReadLine
                strAnsD = InputFile.ReadLine

Perhaps a sample of your text file will help to clarify what you need.

Yes, a sample file would help. If each block is one question with multiple answers then the trick becomes keeping track of where you are in the block and handling improper format such as - if you need four possible answers and the file only has three then how do you detect that and how do you handle it once detected. My suggestion is to load the entire quiz into memory (parsed) up front so that you know that the entire file is valid prior to starting the quiz.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.