Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fwrite and file 3

Status
Not open for further replies.

itflash

Programmer
Jul 18, 2001
535
GB
Hi,

I want to replace a line in a text file.
However, there does not seem to be a function to do this.
Any ideas?

Thanks
 
itflash

The following is somewhat unsophisticated but works.

Convert the text file to a table, locate the text file line, (record), replace with new string and convert back to a text file

CREA TABLE temp (cLine c(254))
APPE FROM MyText.txt DELIMITED
SELE TEMP

* LOCATE FOR etc
* REPLACE MyText.cline WITH etc

lcFile = []
SCAN
[tab]lcFile = lcFile + ALLT(TEMP.cLine) + CHR(13)
ENDS
STRTOFILE(lcFile,[MyText.txt])

Chris :)
 
How do you identify the line you want to replace? And what sort of a file is it? It's pretty easy to slice and dice a text file, but a little info would be helpful.

Have you looked at FILETOSTR() ALINES() and STRTOFILE()? They make it pretty easy to work with a file on a line to line basis.

Dave Dardinger
 
An easier way in VFP 6.0+, is to use the FILETOSTR() and STRTOFILE(). These with STRTRAN() could do it in 3 lines.

mystring = FILETOSTR("mytext.txt")
mystring = STRTRAN(mystring, "looking for" ,"replace by")
=STRTOFILE(mystring, "mytext.txt")

Rick





 
Thanks for the replies

The best way seems to be STRTOFILE for me, the only issue I have is if there is a limitation on the length of a text string.

254 char?

But it works for my file anyway, which is 2500 characters.
 
I knew I'd seen it before. According to the programmers guide, appendex "Visual FoxPro System Capabilities" the maximum number of characters of a character string or memory variable is ... 16,777,184. IOW no need to worry unless you're trying to put the library of congress into one variable. BTW, the programmer's guide can be found under 'Using Visual FoxPro' in the documentation.

Dave Dardinger
 
Rick's use of STRTRAN() is the shortest way to achieve the result ITflash is looking for.

The conversion of the text file to a table idea has merit in that you can additionally insert lines into a text file through the use of the INSERT command.

What I tend to forget is that the INSERT command, (not to be confused with INSERT- SQL), is no longer documented, and there may be those unfamiliar with its syntax.

GO 123
INSERT BLANK
REPL TABLENAME.fieldname WITH [Hello World]

will insert a blank record immediately after record 123, anabling the REPLACE to take effect into the new blank record.

The other syntax is

INSERT BLANK BEFORE

which will open up the record in the Edit window.

One important thing to note is that INSERT BLANK [BEFORE] will not work on buffered data.

HTH

Chris :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top