Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

How to Insert a line of text on line 20 of the text file ?

I have a text file that has text in it and I want to goto the end of line 1 and
do a ret so line to is blank and then I want to insert a line of text on line 2
the do a save. In Visual Basic 6.

Thanks
0
delflyzero
Asked:
delflyzero
  • 4
  • 3
  • 2
  • +1
1 Solution
 
bobbit31Commented:
this is untested, but it should work

Dim ff as Integer
Dim strFileContents as String
Dim intLineNum as Integer
Dim strCurLine as String

ff = FreeFile

Open "C:\my documents\test.txt" For Input as #ff

do while not eof(ff)
   Line Input #ff, strCurLine
   intLineNum = intLineNum + 1
   if intLineNum = 1 then
      strFileContents = strFileContents & strCurLine & vbCrLf & "TEXT YOU WANT TO ADD"
   end if  
loop

close(ff)
ff = FreeFile

Open "C:\my documents\test.txt" For Output as #ff
print #ff, strFileContents
close(ff)

0
 
tolstoyleoCommented:
no... that's not how to do it... the real problem is... (and i faced this one before which took me weeks before i figured it out) is that you have to sort of copy the first line, and copy the 2nd line in different variables and rewrite the whole file with 1st line variable, 2nd line is whatever you want to insert, and 3rd line would be the 2nd variable:

working example:

Private Sub Form_Load()
'crappy example but works
Dim firstStr As String
Dim secondStr As String
Dim i As Integer
i = 1
Dim fso As Object
Dim fs As Object
Set fso = CreateObject("scripting.filesystemobject")
Set fs = fso.opentextfile("c:\1.txt", 1)
While Not fs.atendofstream
If i = 1 Then firstStr = fs.readline
If i = 2 Then secondStr = fs.readline
i = i + 1
Wend
Set fs = Nothing

Set fs = fso.createtextfile("c:\1_copy.txt")
fs.writeline (firstStr)
fs.writeline "STRING YOU WANT TO INSERT"
fs.writeline (secondStr)

Set fso = Nothing
End Sub




The text file i used in this example only has 2 lines of text

the first line of text is: test1
the second line of text is: test2
0
 
Hornet241Commented:
If you want to eraase the first line and insert a line between line 1 and 2 then this would be the easiest

ff = freefile
open "C:\test.txt" for input as #ff
gg = freefile
open "C:\test1.text" for output as #gg

cnt = 1
do while not eof(ff)
    line input #ff, strOrgin
    Select Case cnt
        Case is = 1
            Print #gg, "" ' erase line 1
        Case is = 2
            Print #gg, "Your new data" ' insert your data
            Print #gg, strOrgin ' old line 2
        Case else
            Print #gg, strOrgin ' the rest of the file
    End Select
loop
Close #ff
Close #gg

' delete the old file rename the new file
kill "C:\Test.txt"
Name "C:\Test1.txt" as "C:\Test.txt"

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Hornet241Commented:
I forgot to put the following line after the  'End Select'

     cnt = cnt + 1

this is the line counter.

Also by adding to the Select Case you can insert and delete any line number you want
0
 
bobbit31Commented:
> no... that's not how to do it... the real problem is... (and i faced this one before which took me weeks before i figured it out) is that you have to sort of copy the first line, and copy the 2nd line in different variables and rewrite the whole file with 1st line variable, 2nd line is whatever you want to insert, and 3rd line would be the 2nd variable:

you aren't referring to my code are you? it does basically the same as you suggest above, except that it copies the rest of the file (after line 1 and 2) to the new file, thereby doing an "insert" of a line.
0
 
delflyzeroAuthor Commented:
this is what I want on line 2
<!DOCTYPE ArticleSet PUBLIC "-//NLM//DTD PubMed 2.0//EN" "http://www.ncbi.nlm.nih.gov:80/entrez/query/static/PubMed.dtd">
line 1 should stay as it is line2 should be come line3
and so on for the rest of the lines
I'm also having a problems with the " and dashs

Case Is = 1
            Print #gg, strOrgin ' old line 1
       Case Is = 2
           Print #gg, " <!DOCTYPE ArticleSet PUBLIC "-//NLM//DTD PubMed 2.0//EN" "http://www.ncbi.nlm.nih.gov:80/entrez/query/static/PubMed.dtd">" ' insert your data
           Print #gg, strOrgin ' old line 2
       Case Else
           Print #gg, strOrgin ' the rest of the file
0
 
delflyzeroAuthor Commented:
BTW this will be an XML file type I just used txt as an example
0
 
delflyzeroAuthor Commented:
BTW this will be an XML file type I just used txt as an example
0
 
bobbit31Commented:
ok, this is tested and it works:

Dim ff As Integer
Dim strFileContents As String
Dim intLineNum As Integer
Dim strCurLine As String
Dim strTextToAdd As String

strTextToAdd = "<!DOCTYPE ArticleSet PUBLIC " & Chr(34) & "-//NLM//DTD PubMed 2.0//EN" & Chr(34) & " " & Chr(34) & "http://www.ncbi.nlm.nih.gov:80/entrez/query/static/PubMed.dtd" & Chr(34) & "> "


ff = FreeFile

Open "C:\my documents\test.txt" For Input As #ff


Do While Not EOF(ff)
  Line Input #ff, strCurLine
  intLineNum = intLineNum + 1
  If intLineNum = 1 Then
     strFileContents = strFileContents & strCurLine & vbCrLf & strTextToAdd & vbCrLf
  Else
     strFileContents = strFileContents & strCurLine & vbCrLf
  End If
Loop

Close (ff)
ff = FreeFile

Open "C:\my documents\test.txt" For Output As #ff
Print #ff, strFileContents
Close (ff)
0
 
delflyzeroAuthor Commented:
Thanks Very Much
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now