On 15 Oct 96 Ben Cavanagh said to All...
BC> Does somebody know if there's a function to check if a file of any
BC> extension is a DBF file. Because when I'm opening a non-dbf file
BC> with the command (USE) my program stop and say a error message.
I wonder why you allow your users to create .DBF files with other
extensions ?
You might try testing the file by reading in the first few bytes of the file
with FREAD. You could check the first byte. If this is a regular Clipper
.DBF file using dBase 3+ format, then bits 0,1,2 (values 1,2,4) should total
to 3.
Something like this (warning - untested code, for example only) ...
nHandle := FOPEN(cFileName)
cBuf := SPACE(4) // pre-allocate some memory (else VMIF !)
FREAD(nHandle,@cBuf,4) // must be "@cBuf", not "cBuf"
FCLOSE(nHandle)
nAsc := ASC(cBuf) // ASCII value of 1st char. only
nAsc := nAsc % 8 // modulo 8, i.e. 0..7, takes 3 low bits
IF nAsc == 3 etc. ........... // then this MAY be a Clipper .DBF
Also the next three bytes, which are the "last update" date, should
translate to a reasonable date. I believe the bytes are year, month, day in
that order. So the second byte (year) should be say 90..99 or 0..10, the
third byte (month) should be in the range 1..12, and the fourth byte (day)
should be in the range 1..31. You may need to reset the clock on your PC
and update a DBF file, then use DEBUG to find out if year 2001 is listed as
01 or 101 in the DBF's header. It is also a good idea to get the system
date from the PC's clock, and to scream murder if the year is 1980 (clock
not set, in which case last update date is probably also invalid).
IF YEAR(DATE()) < nYearIWroteCode .... // e.g. 1996 - then scream at user !
nAsc := ASC(SUBSTR(cBuf,2,1))
IF ((nASC > 90) .AND. (nAsc < 100)) .OR. (nASC < 10) ...... Year looks OK
nAsc := ASC(SUBSTR(cBuf,3,1))
IF (nASC > 0) .AND. (nAsc < 13) ...... Month looks OK
nAsc := ASC(SUBSTR(cBuf,4,1))
IF (nASC > 0) .AND. (nAsc < 32) ...... Day looks OK
--- PPoint 2.00
---------------
* Origin: Kingston Canada (1:249/109.11)
|