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.
13 16761
Can't do that with text file.......
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. -
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
-
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. -
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
-
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.
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.
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. -
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
-
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.
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.
- Sub Test()
-
Dim file_name As String
-
-
file_name = "C:\Documents and Settings\WinblowsME\Desktop\Input.txt"
-
-
Call Insert_Line(file_name, 3, "INSERT INTO LINE 3")
-
Call Insert_Line(file_name, 5, "INSERT INTO LINE 5")
-
Call Insert_Line(file_name, 7, "INSERT INTO LINE 7")
-
End Sub
-
-
Private Sub Insert_Line(file_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
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.
The code is not very efficient though. I'm thinking that the dynamic array is not needed...
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.
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.
you can use textstream for that
Sign in to post your reply or Sign up for a free account.
Similar topics
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 ...
|
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...
|
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?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |