reading different sections of a text file at a time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mustakbal
    New Member
    • Jul 2007
    • 25

    #1

    reading different sections of a text file at a time

    How can I read only a section of a text file at a time.Say I have a text file of 1000 rows as database and I want to read from row 1 to 200 only, then maybe from 201- 275 and so on.
    What is the code that would use if I create a button that says 1-200.

    Thanks
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Originally posted by mustakbal
    How can I read only a section of a text file at a time.Say I have a text file of 1000 rows as database and I want to read from row 1 to 200 only, then maybe from 201- 275 and so on.
    What is the code that would use if I create a button that says 1-200.

    Thanks
    This is a nicely detailed post, mustakbal!

    I will try to see if can find you an example, something may already been posted I'll search there too.

    Please stay tuned, welcome!
    Last edited by Dököll; Jul 4 '07, 03:36 PM. Reason: Text

    Comment

    • Dököll
      Recognized Expert Top Contributor
      • Nov 2006
      • 2379

      #3
      Just tried and it works at my end:

      (1) Add a command button to your form, add a loop
      (2) I have three labels, you can modify, Label1(0), and so on...
      (3) You can dimension found, Linenum, and pos, Dim as integer seems to work okay

      The result is you get the number of line in the document red through a multiline box (Text1.Text). Sorry this is reading it reverse, could not find a good example
      Code:
      Do Until found = 0
         found = InStrRev(Text1.Text, Chr(10), pos, 0)
         If found = 0 Then Exit Do
         Linenum = Linenum + 1
         pos = found - 1
      Loop
      Label1(0).Caption = Linenum
      You should also assign values:

      Code:
      found = -1    
      Linenum = 1  
      pos = Text1.SelStart
      Add a lengthy letter in the Text1.Text mutiline textbox and fire command button, Label1(0) should tell you how many lines are in the document.

      There are other ways to do this, please let us know.
      Last edited by Dököll; Jul 4 '07, 04:27 PM. Reason: Text, Code

      Comment

      • mustakbal
        New Member
        • Jul 2007
        • 25

        #4
        Thanks Dököll for your offer to help. I have been trying to make the code above work with no success yet. But I still don't see where I am setting the limit of high and low number of rows. By the way I am a newbie also in vb6 ! so you might need to do the step-by-step instructions with me!

        When I open a file that has 1000 lines in it and I declare the number of lines as 1000, and then wanted to randomly call different lines, it will be picking them from among the numbers 1 through 1000. Now, how do I set the upper and lower limits (say 200 as the low and 600 as high) and have the program call randomly only from the lines 200 throgh 600.

        If I can do that with this code where would the 200 and 600 go in the code.

        Thanks in advance.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          You could use any code you like to read the file (there's a simple code snippet to do this in the Articles section - you can find it in the Index up the top of this forum).

          Assuming you are reading from the start of the file, all you have to do is count the lines as you read them. If you want to process lines 201-700, just skip each line until your count reaches 201. Then when your count hits 701, drop out of the loop.

          It would probably be both faster and more efficient (unless the file is too large) to just read all of the lines into an array. Then you can pick and choose from the array, however you like.

          On the other hand, if the lines are all the same length you could open the file in "random access" mode which allows you to just straight to any line, by number. In other words, you could tell VB to open the file, jump to the 384th line, read it, and close the file, without accessing any other lines in the file. But only if the lines are of identical length. (Note that if you did want to go this way you could always pad the lines with spaces or something to make them the same length.

          Comment

          • mustakbal
            New Member
            • Jul 2007
            • 25

            #6
            O K,Killer42..we are on the right track but please keep in mind that you are dealing with a newbie. The part about : "If you want to process lines 201-700, just skip each line until your count reaches 201. Then when your count hits 701, drop out of the loop." is what I am interested in doing..but how do I set these limits in the progrogram and then have that followed by the program choosing entries at random from this set only (201-700).Does the program have to go through the skipping procedure each time it chooses another random entry or is there a better way of doing that.

            That's where I am stuck right now trying to write the code
            1- set the limits and
            2- have entries randomly chosen from between these numbers.

            By the way I have the code for choosing random entries from the whole file but not parts.

            Thanks

            Comment

            • 1fromOZ
              New Member
              • Jul 2007
              • 2

              #7
              [CODE=vb]'Try This

              'On your Form
              ' Place a command button called CmdRead
              ' Place three text boxes called txtMyFile, txtstartline, txtendline, txtReadLine
              ' Type your file name (including path and the extension of the file) in txtMyFile
              ' or alternatively type the file name (including path and the extension of the file) in quotation marks inplace of txtMyFile.text like "C:\Data\myFile NametoRead.txt"
              ' Type first and last line numbers you want to read in txtstartline and txtendline respectively
              ' Click CmdRead

              'Unfortunately VB cannot handle fixed field length sequential files like QuickBasic used to
              ' because of that we can not jump to a location in a file


              Private Sub CmdRead_Click()
              Dim startline, endline, currentLine As Integer
              Dim LineRead As String
              startline = txtstartline: endline = txtendline
              currentLine = 0:
              If startline < 1 Then startline = 1
              If endline < 1 Then endline = 1

              Close #1: Open txtMyFile.Text For Input As #1

              Do While currentLine < startline
              If EOF(1) <> -1 Then Input #1, LineRead Else GoTo endFile: 'Check if end of file
              currentLine = currentLine + 1
              Loop

              Do Until currentLine > endline
              If EOF(1) <> -1 Then Input #1, LineRead Else GoTo endFile: 'Check if end of file
              txtReadLine.Tex t = CStr(currentLin e) & ": " & LineRead
              MsgBox CStr(currentLin e) & " OK", vbOKOnly
              currentLine = currentLine + 1
              Loop

              Finish:
              MsgBox "All Done", vbOKOnly
              Exit Sub

              endFile:
              MsgBox "end of File", vbOKOnly

              End Sub[/CODE]

              Comment

              • mustakbal
                New Member
                • Jul 2007
                • 25

                #8
                1fromOZ]
                This worked great... but can I code a button so that clicking it would set the lower and upper limits as 1-100 and another button as 101-250 and so on. and have this code in the program so that if I wanted to work with 101-250 set I would click the second button.
                I think the answer is in your code but I haven't figured it ouit yet.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by 1fromOZ
                  Unfortunately VB cannot handle fixed field length sequential files like QuickBasic used to because of that we can not jump to a location in a file
                  I'm afraid that is simply not true.

                  VB probably does handle them slightly differently - it's been quite a few years, I can't remember QB in that much detail. But you can open as random (random-access fixed-length record file) and jump directly to any record you want. If you open as Binary, you can jump directly to any byte in the file and start reading.

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by mustakbal
                    By the way I have the code for choosing random entries from the whole file but not parts.
                    Surely all you have to do, then, is check that the number of the line you are going to retrieve is within the desired limits. You shouldn't need to make any change in the file handling.

                    Let's assume you have two variables called LowerLimit and UpperLimit, defining the section of the file you're allowed to read. You then decide to go and get line number RequestedLineNu mber...

                    [CODE=vb]
                    Select Case RequestedLineNu mber
                    Case LowerLimit To UpperLimit
                    ' Do whatever it is you do now.
                    Case Else
                    ' Return an error, or whatever.
                    End Select[/CODE]
                    You could also have used an IF...ELSE of course. I just happen to like Select Case because of the way it allows you to define a range using the To clause.

                    Comment

                    • mustakbal
                      New Member
                      • Jul 2007
                      • 25

                      #11
                      It seems like this should work..unfortuna tely I can't check it right now because I have to leave for a couple of hours.
                      I'll check it later and post my progress..Pleas e check back later to see if I am stuck again!!
                      Appreciate all your help and suggestions.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Glad to help. :)

                        Even if you don't need any more help, pop back and let us know how it went.

                        Comment

                        • mustakbal
                          New Member
                          • Jul 2007
                          • 25

                          #13
                          I am back ..it seems like after I set the upper and lower limits, the random statement for choosing the next item doesn't work anymore:

                          Int ( Rnd * uppernumber ) +1

                          as this was useful for numbers between 1 and 1000. What needs to be changed here to control that the numers that are chosen are between 600 and 900 for example?

                          Comment

                          • mustakbal
                            New Member
                            • Jul 2007
                            • 25

                            #14
                            I got it ! finaly.. I had to change the statement to read
                            lowerlimit+ Rnd * (upperlimit - lowerlimit)

                            and it seems to work..

                            Thanks again for your help and I '' be dropping by to learn from this forum...Seems people quite helpful here.

                            Comment

                            • Killer42
                              Recognized Expert Expert
                              • Oct 2006
                              • 8429

                              #15
                              Originally posted by mustakbal
                              I got it ! finaly.. I had to change the statement to read
                              lowerlimit+ Rnd * (upperlimit - lowerlimit)
                              and it seems to work..
                              Well done! :)

                              There's a code sample you could have copied from our VB Articles section which provides a function to return a random number within specified limits. But it just does the same thing you described here, and I believe it's much more satisfying when you work it out for yourself. (One thing which may be of interest - I've recently added another version which doesn't repeat numbers.)

                              Originally posted by mustakbal
                              Thanks again for your help and I '' be dropping by to learn from this forum...Seems people quite helpful here.
                              That's why the forum's here. :)

                              Most people (including myself) first come here to ask a question, and eventually move on to answering them. Just pop in when you can spare the time, and before you know it you'll be spotting questions that make you think "hang on, I know that".

                              Comment

                              Working...