By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

Reading Certain lines in a text file

100+
P: 133
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.
Jan 24 '08 #1
Share this Question
Share on Google+
13 Replies


P: 7
Can't do that with text file.......
Jan 24 '08 #2

P: 58
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.
Expand|Select|Wrap|Line Numbers
  1.    Dim line1 As String, line2 As String, line3 As String
  2.  
  3.    Open App.Path & "\Temp.txt" For Input As #1
  4.       Line Input #1, line1
  5.       Line Input #1, line2
  6.       Line Input #1, line3
  7.    Close #1
  8.  
  9.    Debug.Print line1 & Chr(13) & line2 & Chr(13) & line3
  10.  
Jan 25 '08 #3

Expert 5K+
P: 8,434
Subscribing .
Jan 27 '08 #4

100+
P: 133
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.
Expand|Select|Wrap|Line Numbers
  1.    Dim line1 As String, line2 As String, line3 As String
  2.  
  3.    Open App.Path & "\Temp.txt" For Input As #1
  4.       Line Input #1, line1
  5.       Line Input #1, line2
  6.       Line Input #1, line3
  7.    Close #1
  8.  
  9.    Debug.Print line1 & Chr(13) & line2 & Chr(13) & line3
  10.  

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.
Jan 29 '08 #5

Expert 5K+
P: 8,434
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.
Jan 29 '08 #6

100+
P: 133
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.
Expand|Select|Wrap|Line Numbers
  1.    Dim line1 As String, line2 As String, line3 As String
  2.  
  3.    Open App.Path & "\Temp.txt" For Input As #1
  4.       Line Input #1, line1
  5.       Line Input #1, line2
  6.       Line Input #1, line3
  7.    Close #1
  8.  
  9.    Debug.Print line1 & Chr(13) & line2 & Chr(13) & line3
  10.  

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.
Jan 29 '08 #7

Expert 5K+
P: 8,434
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.
Jan 29 '08 #8

P: 58
Expand|Select|Wrap|Line Numbers
  1. Sub Test()
  2.    Dim file_name As String
  3.  
  4.    file_name = "C:\Documents and Settings\WinblowsME\Desktop\Input.txt"
  5.  
  6.    Call Insert_Line(file_name, 3, "INSERT INTO LINE 3")
  7.    Call Insert_Line(file_name, 5, "INSERT INTO LINE 5")
  8.    Call Insert_Line(file_name, 7, "INSERT INTO LINE 7")
  9. End Sub
  10.  
  11. Private Sub Insert_Line(file_name As String, line_num As Long, str_line As String)
  12.    Dim lines() As String, i As Long
  13.  
  14.    i = 1
  15.  
  16.    On Error GoTo ERROR_HANDLER
  17.  
  18.    Open file_name For Input As #1
  19.       Do While Not EOF(1)
  20.          ReDim Preserve lines(i)
  21.          Line Input #1, lines(i)
  22.          i = i + 1
  23.       Loop
  24.    Close #1
  25.  
  26.    If i = 1 Or line_num > i Then
  27.       Open file_name For Append As #1
  28.          Print #1, str_line
  29.       Close
  30.    Else
  31.       Open file_name & "_TEMP" For Output As #1
  32.          For i = 1 To UBound(lines)
  33.             If i <> line_num Then
  34.                Print #1, lines(i)
  35.             Else
  36.                Print #1, str_line
  37.                Print #1, lines(i)
  38.             End If
  39.          Next i
  40.       Close
  41.  
  42.       Kill file_name
  43.       Name file_name & "_TEMP" As file_name
  44.    End If
  45.  
  46.    Exit Sub
  47. ERROR_HANDLER:
  48.  
  49. End Sub
Jan 30 '08 #9

Expert 5K+
P: 8,434
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.
Jan 30 '08 #10

P: 58
The code is not very efficient though. I'm thinking that the dynamic array is not needed...
Jan 30 '08 #11

Expert 5K+
P: 8,434
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.
Jan 31 '08 #12

100+
P: 133
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.
Jan 31 '08 #13

xtianixm
P: 52
you can use textstream for that
Jan 31 '08 #14

Post your reply

Sign in to post your reply or Sign up for a free account.