* Your file name: 'cvtoracl.txt'
set step on
private cFile1, cFile2, cFile3
private cTmpCurs, cTmpFile1, cTmpFile2, nWkArea, nTmpFlHndl
* Get temporary drive and directory
* Work around for Fox2X's sys(2023) command
* only returning the drive and not the path
cTmpCurs = sys(2015)
cTmpFile1 = cTmpCurs && Creates a file to test empty and open logic below
cTmpFile2 = cTmpFile1 + "A"
nWkArea = select()
create cursor (cTmpCurs) (fld c(1))
cTmpFile1 = left(dbf(),rat("\",dbf())) + cTmpFile1 + ".txt"
cTmpFile2 = left(dbf(),rat("\",dbf())) + cTmpFile2 + ".txt"
use in (cTmpCurs)
select (nWkArea)
*
cFile1 = cTmpFile2
cFile2 = ""
cFile3 = cTmpFile1
* Create test files...
nTmpFlHndl = fcreate(cTmpFile1)
=fclose(nTmpFlHndl)
nTmpFlHndl = fcreate(cTmpFile2)
=fwrite(nTmpFlHndl,"Hello")
=fclose(nTmpFlHndl)
* Test function...
private cBytes, i
for i = 1 to 3
if i == 3
* Cause open file failure...
nTmpFlHndl = fopen(cTmpFile1)
endif
cBytes = ReadAFile(eval("cFile"+str(i,1)))
do case
case type("cBytes")=="N"
wait "Can't open file!" window timeout 3
case type("cBytes")=="L"
wait "File doesn't exist!" window timeout 3
case empty(cBytes)
wait "File is empty!" window timeout 3
otherwise
* Process valid file bytes...
endcase
next
=fclose(nTmpFlHndl)
delete file (cTmpFile1)
delete file (cTmpFile2)
function ReadAFile
parameters cFileName
private cFileName, nFlHndl, nSize, vFileBytes, bFileExists
vFileBytes = ""
bFileExists = file(cFileName)
if bFileExists
nFlHndl = fopen(cFileName)
if nFlHndl <> -1
nSize = fseek(nFlHndl,0,2) && Get file size
=fseek(nFlHndl,0,0) && Reposition the file pointer
vFileBytes = fread(nFlHndl,nSize) && Read the file
=fclose(nFlHndl) && Close it
endif
endif
return iif(!bFileExists, bFileExists, iif(nFlHndl<>-1, vFileBytes, nFlHndl) )