Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Find and Replace in huge text file
Message
From
27/09/2005 13:13:56
 
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
01053411
Message ID:
01053547
Views:
19
>>I tried FREAD instead of FGETS and it looks like it works fine, I tried to read 8192 bytes with FREAD then write them with FWRITE without any modifications and I got same file size.
>
>With FREAD some occurences may be split between two sequential reads in which case they will not be replaced.

Not tested, but I wonder if this might be a faster approach?
LPARAMETERS cInFile, cOutFile, cFind, cReplace

LOCAL  cVar, aPos[1], nCtr, nOccurs, nHndIn, nHndOut, nEnd

m.nHnd = 0
m.nHndOut = 0
m.cVar = FILETOSTR(m.cInFile)
m.nOccurs = OCCURS(m.cFind,m.cVar)

FOR m.nCtr = 1 TO m.nOccurs
   aPos[m.nCtr] = IIF(m.nCtr > 1,;
                      AT(m.cFind,m.cVar,m.nCtr) - aPos[m.Ctr - 1], ;
                      AT(m.cFind,m.cVar,m.nCtr) - LEN(m.cFind))
ENDFOR 

DO WHILE m.nHndOut < 1
   m.nHndOut = FCREATE(m.cOutFile)
ENDFOR    

DO WHILE m.nHndIn < 1
   m.nHndIn = FOPEN(m.cInFile)
ENDDO 

m.nEnd = FSEEK(m.nHndIn,0,2)
= FSEEK(m.nHndIn,0,o)

= FWRITE(m.nHndOut,FREAD(m.nHndIn,LEN(m.cFind)),LEN(m.cFind))

FOR m.nCtr = 1 TO m.nOccurs
   = FWRITE(m.nHndOut,STRTRAN(FREAD(m.nHndIn,aPos[m.nCtr]),m.cFind,m.cReplace)aPos[m.nCtr])
ENDFOR    

= FWRITE(m.nHndOut,FREAD(m.nHndIn,m.nEnd-FSEEK(m.nHndIn,0,1)),m.nEnd-FSEEK(m.nHndIn,0,1))

= FCLOSE(m.nHndIn)
= FCLOSE(m.nHndOut)
Of course, it would only work if the strings being replaced were the same length.

Regards,
Jim
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform