|
-
Oct 30th, 2009, 02:02 AM
#1
Thread Starter
Lively Member
(VB6) How to append text before first line in a text file ?
Hi all,
I need to append a text file using generic file I/O commands in VB6.
How do I append my text to first position in text file?
I tried working like :
Code:
Dim FileNo As Integer
Dim FilePath As String
FileNo = FreeFile
FilePath = "C:\Samplefile.txt"
Open FilePath For Binary As FileNo
Seek FileNo, 1
Put FileNo, , "First Text" & vbCrLf
Put FileNo, , "Second Text" & vbCrLf
Close FileNo
but this overwrite the first line in my text file.
Regds,
-
Oct 30th, 2009, 02:25 AM
#2
Re: (VB6) How to append text before first line in a text file ?
something like this...
Code:
Private Sub Command1_Click()
Dim FilePath As String
Dim StrVal As String
FilePath = "C:\Samplefile.txt"
Open FilePath For Input As #1
StrVal = Input(LOF(1), #1)
Close #1
Open FilePath For Output As #1
Print #1, "First Text"
Print #1, "second Text"
Print #1, StrVal
Close #1
End Sub
-
Oct 30th, 2009, 02:56 AM
#3
Thread Starter
Lively Member
Re: (VB6) How to append text before first line in a text file ?
Hey hi seenu,
thanks for the reply,
one thing is in my mind don't you think there isn't any way besides reading the whole file and writing it back ?
Regds,
-
Oct 30th, 2009, 04:23 AM
#4
Re: (VB6) How to append text before first line in a text file ?
The fastest way is to read the text file into an array, open a new text file, write your text to the first line and then append the array to the text file...
here is the code to read the file into the array in 1 go....
Code:
Dim FilePath As String
FilePath = "C:\Samplefile.txt"
'~~> Open file and read it into an array
Open FilePath For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
Last edited by Siddharth Rout; Oct 30th, 2009 at 04:28 AM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Oct 30th, 2009, 05:19 AM
#5
Thread Starter
Lively Member
Re: (VB6) How to append text before first line in a text file ?
Hi koolsid,
Well I need to know how to write a text to the first postion in a previous written file without reading the whole file and write it back.
Regds,
-
Oct 30th, 2009, 05:51 AM
#6
Re: (VB6) How to append text before first line in a text file ?
Appending to a location that is not the end of the file will require to read the whole file in, update it, and then write it back to file.
What you can do is read file into two arrays, insert the new data into the begining of the array followed by rest of the text and update it back to the file...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Oct 30th, 2009, 06:28 AM
#7
Thread Starter
Lively Member
Re: (VB6) How to append text before first line in a text file ?
Hi KoolSid,
Well I guess ,, there isn't any way to just append some text on the first position in a file too.
Well I worked as like this too.
Thanks
Regds,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|