Text file:
test1
test2
test3
test4
I want to write after "test2" - something. I tried first time to open the file and I read the position where is test2. After that I opened the text file in Append format but I don't know how to write on specific position.
I must to use next commands or others in Visual Basic:
for reading from a text file: Open Test_Filename For Input As #
for appending in a text file: Open Test_Filename For Append As #3
Any helps will be great.
Edit:
LineNum = 1
LineRead = 0
Test_Filename ="path/test.txt"
If Len(Dir$(Test_Filename)) = 0 Then
MsgBox ("Cannot find the file")
End If
Open Test_Filename For Input As #3
Do While Not EOF(3)
LineNum = 1
strData1 = ""
LineNext = 0
For LineNum = 1 To 2
If EOF(3) Then
Exit For
End If
Line Input #3, strLine
On Error GoTo 0 'reset error handling
LineRead = LineRead + 1
If Left(strLine, 4) = "test1" Then
strData1 = "test1"
LineNum = 1
End If
If (strData1 = "test1") And (Left(strLine, 2) = "test2") And (LineNum = 2) Then
strData2 = strData2 & strLine + vbCrLf
strData1 = ""
Else
strData2 = ""
End If
Next LineNum
Loop
Close #3
Open Test_Filename For Append As #3
If (InStr(strData2, "test2") <> 0) Then
Print #3, "Something"
Else
Print #3, "Error"
End If
Close #3
In this code I want to write after "test2" and other condition "Something". I must to open the file twice time because when I open with "For Input" I can't to write in the file.
But in this situation I wrote on the end of the file "Something" and I must to write to a specific position, after "test2".