473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading Certain lines in a text file

133 100+
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
13 16761
SonnyH
7
Can't do that with text file.......
Jan 24 '08 #2
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
Killer42
8,435 Expert 8TB
Subscribing .
Jan 27 '08 #4
gobblegob
133 100+
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
Killer42
8,435 Expert 8TB
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
gobblegob
133 100+
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
Killer42
8,435 Expert 8TB
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
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
Killer42
8,435 Expert 8TB
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
The code is not very efficient though. I'm thinking that the dynamic array is not needed...
Jan 30 '08 #11
Killer42
8,435 Expert 8TB
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
gobblegob
133 100+
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
you can use textstream for that
Jan 31 '08 #14

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

Similar topics

7
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
15
by: djj858 | last post by:
Another newbie question: How do I begin reading data, but starting from the xth line down a list? In other words, how do I skip the first lines and not read in those values?
11
by: Matt DeFoor | last post by:
I have some log files that I'm working with that look like this: 1000000000 3456 1234 1000000001 3456 1235 1000020002 3456 1223 1000203044 3456 986 etc. I'm trying to read the file...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
6
by: zoneal | last post by:
Size of text file to read and display can be larger than 10MB. Since I must ignore certain input text lines the code I'm using is: FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input) 'Read...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
17
by: swuwarrior | last post by:
Whats up guys. I just decided i would try porgramming as i am a physical education major. As apart of my major i deal with alot of paper work. So i wanted to write a program to mess with all the...
2
by: friend.blah | last post by:
i have a text file lets say in this format abc abs ajfhg agjfh fhs ghg jhgjs fjhg dj djk djghd dkfdf .... .... ...... i want to read the first line at certain time for eg : at 10clk
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.