Reading Certain lines in a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gobblegob
    New Member
    • Dec 2007
    • 133

    Reading Certain lines in a text file

    Hi,
    I would like to know how i could read and write in a specific line in a text file with VB6.

    eg.

    first line
    second line
    third line

    I dont want to read through all lines, I just want to read a certain line as an input and write to a certain line as output.


    Thanks
    Gobble.
  • SonnyH
    New Member
    • Nov 2007
    • 7

    #2
    Can't do that with text file.......

    Comment

    • WinblowsME
      New Member
      • Jan 2008
      • 58

      #3
      Here's the code to read in certain lines from a file. As for writing to a certain line in a file, do you want to write to the file you're reading? Give us an example.
      [code=vb]
      Dim line1 As String, line2 As String, line3 As String

      Open App.Path & "\Temp.txt" For Input As #1
      Line Input #1, line1
      Line Input #1, line2
      Line Input #1, line3
      Close #1

      Debug.Print line1 & Chr(13) & line2 & Chr(13) & line3
      [/code]

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Subscribing .

        Comment

        • gobblegob
          New Member
          • Dec 2007
          • 133

          #5
          Originally posted by WinblowsME
          Here's the code to read in certain lines from a file. As for writing to a certain line in a file, do you want to write to the file you're reading? Give us an example.
          [code=vb]
          Dim line1 As String, line2 As String, line3 As String

          Open App.Path & "\Temp.txt" For Input As #1
          Line Input #1, line1
          Line Input #1, line2
          Line Input #1, line3
          Close #1

          Debug.Print line1 & Chr(13) & line2 & Chr(13) & line3
          [/code]

          Thanks for the reply its exactly what i wanted ti know,
          Sorry for the late reply but i gave up after getting told it cant be done.
          Thanks again,
          Gobble.

          Thanks for the post mate much appreciated.
          Gobblegob.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by gobblegob
            Thanks for the reply its exactly what i wanted ti know,
            Sorry for the late reply but i gave up after getting told it cant be done.
            Never listen to the people who tell you "it can't be done". They might be right, but if you believe them, you'll never find out.

            Comment

            • gobblegob
              New Member
              • Dec 2007
              • 133

              #7
              Originally posted by WinblowsME
              Here's the code to read in certain lines from a file. As for writing to a certain line in a file, do you want to write to the file you're reading? Give us an example.
              [code=vb]
              Dim line1 As String, line2 As String, line3 As String

              Open App.Path & "\Temp.txt" For Input As #1
              Line Input #1, line1
              Line Input #1, line2
              Line Input #1, line3
              Close #1

              Debug.Print line1 & Chr(13) & line2 & Chr(13) & line3
              [/code]

              Is there a way i can choose a line to output to?

              Dim line1 As String, line2 As String, line3 As String

              Open App.Path & "\Temp.txt" For Output As #1
              Line Output #1, line1
              Line Output #1, line2
              Line Output #1, line3

              But it doesnt work any suggestions?
              Thanks again,
              Gobble.

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                The statement you want there is Print #1, not Line Output #1.

                I believe that what SonnyH was referring to is the difficulty of accessing a specific line in a text file, as opposed to reading or writing all the lines, as you're doing. You see, since the lines can be of different lengths, there's no way VB can tell where any given line starts, except by reading all of the lines up to that point.

                When writing to a text file, things are even worse. There's no convenient way to move chunks of a file around. So if you want to write over the top of an existing line, what happens to all the rest of the data that comes after it?

                There are ways to work around all of this, of course. But it's worth keeping in mind. In fact an upcoming article in the HowTo's area will show an easy way of playing with a text file in this way. And there are articles there already which demonstrate a couple of ways to read files.

                Comment

                • WinblowsME
                  New Member
                  • Jan 2008
                  • 58

                  #9
                  [CODE=vb]Sub Test()
                  Dim file_name As String

                  file_name = "C:\Documen ts and Settings\Winblo wsME\Desktop\In put.txt"

                  Call Insert_Line(fil e_name, 3, "INSERT INTO LINE 3")
                  Call Insert_Line(fil e_name, 5, "INSERT INTO LINE 5")
                  Call Insert_Line(fil e_name, 7, "INSERT INTO LINE 7")
                  End Sub

                  Private Sub Insert_Line(fil e_name As String, line_num As Long, str_line As String)
                  Dim lines() As String, i As Long

                  i = 1

                  On Error GoTo ERROR_HANDLER

                  Open file_name For Input As #1
                  Do While Not EOF(1)
                  ReDim Preserve lines(i)
                  Line Input #1, lines(i)
                  i = i + 1
                  Loop
                  Close #1

                  If i = 1 Or line_num > i Then
                  Open file_name For Append As #1
                  Print #1, str_line
                  Close
                  Else
                  Open file_name & "_TEMP" For Output As #1
                  For i = 1 To UBound(lines)
                  If i <> line_num Then
                  Print #1, lines(i)
                  Else
                  Print #1, str_line
                  Print #1, lines(i)
                  End If
                  Next i
                  Close

                  Kill file_name
                  Name file_name & "_TEMP" As file_name
                  End If

                  Exit Sub
                  ERROR_HANDLER:

                  End Sub[/CODE]

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Thanks Winnie, looks like a handy routine.

                    As you can see from this, gobblegob, the only "convenient " way to replace a line in a text file (unless you can guarantee that the new line is exactly the same length) or to insert a line as this code does, is to make a new copy of the file, with the new line in it.

                    There are quite simple ways to update data inside a file without moving anything around, but if the size of the updated part is to change, then you need some way to move the rest of the data. Making a copy like this is probably about the most common way, as it's probably about the only safe way. ZIP utilities, for instance, do exactly that. If you add, remove or update anything in a ZIP file, the utility simply makes a complete copy of the ZIP archive, with the changes in it. Then it deletes the old copy, and renames the new one.

                    Comment

                    • WinblowsME
                      New Member
                      • Jan 2008
                      • 58

                      #11
                      The code is not very efficient though. I'm thinking that the dynamic array is not needed...

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by WinblowsME
                        The code is not very efficient though. I'm thinking that the dynamic array is not needed...
                        Yeah, it certainly seems as though it would be more efficient to copy directly from one file to the other.

                        Putting them in an array something like that could be useful way to cache the entire file so you only have to read it once. But the existing code obviously doesn't do so.

                        Comment

                        • gobblegob
                          New Member
                          • Dec 2007
                          • 133

                          #13
                          Originally posted by WinblowsME
                          The code is not very efficient though. I'm thinking that the dynamic array is not needed...
                          Wow thanks for information you guys have bundled for me very helpful.
                          Gobble.

                          Comment

                          • xtianixm
                            New Member
                            • Jul 2007
                            • 52

                            #14
                            you can use textstream for that

                            Comment

                            Working...