Using Foxpro to query line lengths
I was reviewing some code changes, and I noticed some of the lines were quite different in length.
I wrote some code to figure out why. I thought initially that I would just create a cursor with a field of length 250 and then use the APPEND FROM …SDF command to put the entire file into a cursor. However, I realized that wouldn’t work because one of the lines was several thousand characters long. So I thought I’d had to use the low level file functions, such as FREAD(). I also thought about appending the entire file into a memo field using the APPEND MEMO command
Then I remembered the ALINES function which served the job quite nicely. I just wanted to show the longest line lengths before and after modifications.
cPath="d:\public\files\"
SET ALTERNATE TO t.txt
SET ALTERNATE on
ShowLens(cPath+"Samples.cpp")
ShowLens(cPath+"Samples2.cpp")
SET ALTERNATE to
_cliptext=FILETOSTR("t.txt")
PROCEDURE ShowLens(cFile as String)
cStr=FILETOSTR(cFile)
n=ALINES(aa,cStr)
CREATE CURSOR lengths (Line i, len i)
FOR i = 1 TO n
INSERT INTO lengths VALUES (i,LEN(aa[i]))
ENDFOR
SELECT TOP 10 line,len FROM lengths ORDER BY 2 DESC INTO CURSOR result
?JUSTFNAME(cFile), "total lines = ",n
LIST off
RETURN