BYTES (return size in bytes)

Top  Previous  Next

 

BYTES(file)

BYTES

Returns number of bytes in FILE, or most recently read.

file

The label of a FILE.

The BYTES procedure returns the size of a FILE in bytes or the number of bytes in the last record successfully accessed. Following an OPEN statement, BYTES returns the size of the file. file{PROP:FileSize} is equivalent to BYTES after a file open and will also return the size of the file. After the file has been successfully accessed by GET, REGET, NEXT, PREVIOUS, ADD, or PUT, the BYTES procedure returns the number of bytes accessed in the RECORD. The BYTES procedure may be used to return the number of bytes read in a variable length record.

In SQL tables, if BYTES(file) is used before the file is opened, then it returns the number of rows in the table. If it is used after the file is opened and a successful record retrieval, it returns the number of bytes in the record buffer.

BYTES() return a negative value for files greater than 2Gb and less than 4Gb. Assigning the result to a ULONG gives the correct positive value of file size.

 

Return Data Type:

LONG

Example:

DosFileName  STRING(260),STATIC

LastRec      LONG

SavPtr       LONG(1)                    !Start at 1

FileSize     LONG

 

DosFile      FILE,DRIVER('DOS'),PRE(DOS),NAME(DosFileName)

Record        RECORD

F1             STRING(2000)

             END

            END

 

BlobStorage  FILE,DRIVER('TopSpeed'),PRE(STO)

File         BLOB,BINARY      

Record        RECORD

FileName       STRING(64)

             END

            END

 

CODE

IF NOT FILEDIALOG('Choose File to Store',DosFileName,,0010b)

 RETURN

END

OPEN(BlobStorage)                      !Open the BLOB file

STO:FileName = DosFileName             !and store the filename

OPEN(DosFile)                          !Open the file

FileSize = BYTES(DosFile)              !Get size of file

STO:File{PROP:Size} = FileSize         !and set the BLOB to store the file

LastRec = FileSize % SIZE(DOS:Record)  !Check for short record at end of file

LOOP INT(FileSize/SIZE(DOS:Record)) TIMES

 GET(DosFile,SavPtr)                   !Get each record

 ASSERT(NOT ERRORCODE())

 !String slice data into BLOB:

 STO:File[SavPtr - 1 : SavPtr + SIZE(DOS:Record) - 2] = DOS:Record

 SavPtr += SIZE(DOS:Record)            !Compute next record pointer

END

IF LastRec                             !If short record at end of file

 GET(DosFile,SavPtr)                   !Get last record

 ASSERT(BYTES(DosFile) = LastRec)      ! size read should match computed size

 STO:File[SavPtr - 1 : SavPtr + LastRec - 2] = DOS:Record

END

ADD(BlobStorage)

ASSERT(NOT ERRORCODE())

CLOSE(DosFile);CLOSE(BlobStorage)

See Also:

OPEN

PROP:FileSize