Click to See Complete Forum and Search --> : file question


lindawqu
July 17th, 2001, 09:18 PM
what is the better way to replace some lines with some new text in a text file?

********************** test.txt ***********
Fill out the form below to post a message on the board. HTML is disabled for this board so you may not use it in your posts. Markup is enabled so you may use Markup on in your posts. Anonymous postings are not allowed, so you will need to register a Username before you can post.

<- new code start ->

Thanks. this is a test
what is this?

<- new test end - >

Tip: You can enclose a block of source code within This will preserve the formatting and add syntax highlighting.
********************************************

I want the following part

<- new code start ->

Thanks. this is a test
what is this?

<- new test end - >

to be replaced by

<- new line start ->

what a nice day!
I will be back soon.

<- new line end - >

Cakkie
July 18th, 2001, 01:00 AM
First read it in a string, then track the <- new code start -> and the <- new test end ->, and replace it with what you want. After that, rewrite the file

dim strFile as string
dim FFile as integer

FFile = FreeFile
Open "file.txt" for input as #FFile

' read the entire file
strFile = input(FileLen("file.txt"),FFile)

Close #FFile

' now do the replacing magic
Dim iPosBeginTag as long
Dim iPosEndTag as long

Dim strSomeOtherText as string
strSomeOtherText = "Whatever needs to com between the new stuff"

iPosBeginTag = Instr(strLine,"<- new code start ->")
iPosEndTag = Instr(strLine,"<- new test end ->") + len("<- new test end ->")

strFile = Left(strFile, iPosBeginTag -1) & _
"<- new line start ->" & _
strSomeOtherText & _
"<- new line end ->" & _
mid(strLine,iPosEndTag)

' now write it back again
FFile = FreeFile
Open "file.txt" for output as #ffile
print #Ffile, strFile
Close #Ffile




Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook