(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,
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
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,
:)
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
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,
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...
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,