Microsoft KB Archive/68159

= How to Truncate a File Using MS-DOS Interrupts from Basic =

Article ID: 68159

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic for MS-DOS
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q68159



SUMMARY
The following Basic code shows how to truncate a file using MS-DOS functions. A more clumsy approach in Basic would be to read a specified amount of records one at a time, writing these records to another file, deleting the first file, and then renaming the new file to the old file. The approach using MS-DOS functions (below) is quicker and more efficient.

The information in this article is also included in the Help file provided with the Standard and Professional Editions of Microsoft Visual Basic for MS-DOS, version 1.0.



MORE INFORMATION
You can invoke MS-DOS service functions using the CALL INTERRUPT statement in Basic.

You can set the size of any file to any arbitrary value by executing MS-DOS Interrupt 21h with service 40h using CX = 00h. The usual technique is to call service 42h to set the file pointer location and then immediately call service 40h with CX = 00h to update the new file size.

The program below is written so that the Truncate% FUNCTION is generic as possible and works with any file. ProcessFile sets up the file-specific information, which in turn calls the Truncate% FUNCTION. Truncate% generates the specific interrupt calls to truncate the file at the specified record. The two SUB programs PrintFile and CreateFile are only needed as an example.

Program Example
To try this example in VBDOS.EXE:
 * 1) From the File menu, choose New Project.
 * 2) Copy the code example to the Code window.
 * 3) Press F5 to run the program.

To run this program in the VBDOS.EXE environment, you must invoke the VBDOS.EXE environment with the /L switch to load the default Quick library. For example:

VBDOS.EXE /L

You must run QB or QBX with the /L option to load the QB.QLB or QBX.QLB Quick library which contains the INTERRUPT routine used in this program. You must LINK with QB.LIB or QBX.LIB when making an .EXE program which uses the CALL INTERRUPT statement. CONST NumRec = 10 TYPE TheType i AS INTEGER END TYPE DIM SHARED TheDim AS TheType ' Use the following include file for Visual Basic for MS-DOS: REM $INCLUDE: 'VBDOS.BI' ' Use the following include file for QuickBasic for MS-DOS: '$INCLUDE: 'QB.BI' ' Use the following include file for Basic PDS for MS-DOS: '$INCLUDE: 'QBX.BI' CALL CreateFile CALL PrintFile CALL ProcessFile(7) CALL PrintFile

'################### File specific SUB ############################# SUB ProcessFile (LastRec%) ' Get the information about &quot;TEST.DAT&quot; and also the information ' about the location of the LASTREC% where the file will be truncated. OPEN &quot;test.dat&quot; FOR RANDOM AS #1 LEN = LEN(TheDim) FilePointer% = LastRec% * LEN(TheDim) IF FilePointer% <> Truncate%(FILEATTR(1, 2), FilePointer%) THEN PRINT &quot;error...&quot; END IF    CLOSE END SUB

'################## Generic INT file truncator #################### FUNCTION Truncate% (handle%, FilePointer%) ' Generic function that will truncate any file at specified offset. ' ' Receives: '      1. a MS-DOS file handle; '      2. a file pointer offset in bytes, pointing where to truncate ' Returns: '      1. If successful, position of file pointer; or '       2. If error, flags register ' DIM Regs AS RegType Regs.ax = &H4200                  ' INT 21h, service 42h Regs.bx = handle%                 ' MS-DOS Handle Regs.dx = FilePointer%            ' Offset in bytes from start. CALL Interrupt(&H21, Regs, Regs) IF Regs.ax <> FilePointer% THEN   ' AX returns pointer position. Truncate% = Regs.flags       '   If error, return flag. END IF    Regs.ax = &H4000                     ' INT 21h, service 40h Regs.bx = handle%                   ' MS-DOS handle Regs.cx = &H0 CALL Interrupt(&H21, Regs, Regs) IF Regs.ax <> 0 THEN              ' Must be zero bytes written. Truncate% = Regs.flags       ' If error, return flag. END IF    Truncate% = FilePointer%          ' Return offset location. END FUNCTION

'############ Create Sample File using RANDOM Access ################ SUB CreateFile KILL &quot;test.dat&quot; OPEN &quot;test.dat&quot; FOR RANDOM AS #1 LEN = LEN(TheDim) FOR j = 1 TO NumRec TheDim.i = j         PUT #1, j, TheDim NEXT j    CLOSE #1 END SUB

'########### Print contents of TEST.DAT ################## SUB PrintFile OPEN &quot;test.dat&quot; FOR RANDOM AS #1 LEN = LEN(TheDim) Max = LOF(1) / LEN(TheDim) FOR j = 1 TO Max GET #1, j, TheDim PRINT TheDim.i    NEXT j     CLOSE END SUB

