⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printdbf.bas

📁 常用基本函数库,也许你需要的正在其中!如果不做程序
💻 BAS
字号:
DEFINT A-ZDECLARE FUNCTION ReadFileStructure% ()DECLARE FUNCTION RightJust$ (Value$, FieldWidth%)DECLARE FUNCTION ZeroJust$ (Number AS INTEGER)DECLARE FUNCTION ReadDbfHdr% ()DECLARE SUB DspDbfInfo ()DECLARE SUB DspFileStructure ()DECLARE SUB Pause ()DECLARE SUB PrintDbfRecord (fv$(), RecNum%)DECLARE SUB PrintReport ()DECLARE SUB ReadDbfRecord (fv$()) '================================================='=   PROGRAM: PRINTDBF.BAS                       ='=   PURPOSE: Print listings of dBASE III+/IV    ='=            DBF files                          ='================================================= '-------------------------------------------------' Initialize variables and create types          -'------------------------------------------------- CONST True = -1, False = 0 TYPE HeaderInfoType   VersionNumber AS INTEGER   LastUpdate    AS STRING * 8   NumberRecords AS LONG   HeaderLength  AS INTEGER   RecordLength  AS INTEGER   NumberFields  AS INTEGER   FileSize      AS LONGEND TYPE TYPE FieldInfoType   FdName   AS STRING * 11   FdType   AS STRING * 1   FdLength AS INTEGER   FdDec    AS INTEGEREND TYPE DIM SHARED Hdr AS HeaderInfoTypeDIM SHARED FileName$ FileName$ = "PLANETS.DBF" '-------------------------------------------------'  Main processing loop                          -'-------------------------------------------------    OPEN FileName$ FOR BINARY AS #1   CLS   ActionHdr = ReadDbfHdr   SELECT CASE ActionHdr      CASE 1         BEEP         PRINT "Not a dBASE III+ or IV file"      CASE ELSE         DspDbfInfo         Pause         DIM SHARED FLDS(Hdr.NumberFields)_                         AS FieldInfoType         ActionFile = ReadFileStructure         SELECT CASE ActionFile            CASE True               CLS               DspFileStructure               Pause               IF ActionHdr <> 2 THEN                  CLS                  PrintReport                  Pause               ELSE                  CLS                  PRINT "No records to print"               END IF            CASE False               BEEP               PRINT "Field information error"            END SELECT   END SELECT   CLOSE #1   END SUB DspDbfInfo  '-------------------------------------------------'Display dBASE file header information           -'------------------------------------------------- PRINT USING "dBASE Version         : #";_                      Hdr.VersionNumberPRINT "Database in use       : "; FileName$PRINT USING "Number of data records: ########";_                             Hdr.NumberRecordsPRINT "Date of last update   : "; Hdr.LastUpdatePRINT USING "Header length         :     ####";_                              Hdr.HeaderLengthPRINT USING "Record length         :     ####";_                              Hdr.RecordLengthPRINT USING "Number of fields      :      ###";_                              Hdr.NumberFieldsPRINT USING "File size             : ########";_                                  Hdr.FileSize END SUB SUB DspFileStructure  '-------------------------------------------------'Purpose: Display the structure of the dBASE file-'         Name, Field Type, Length and number    -'         of decimals if a number                -'------------------------------------------------- FieldTitleS$ =_    "Field  Field Name  Type        Width     Dec"FieldString1$ = "  ###  \         \ "FieldString2$ = "\         \   ###      ##" PRINT : PRINT FieldTitleS$ FOR I = 1 TO Hdr.NumberFields   PRINT USING FieldString1$; I; FLDS(I).FdName;   SELECT CASE FLDS(I).FdType      CASE "C": ty$ = "Character"      CASE "L": ty$ = "Logical"      CASE "N": ty$ = "Number"      CASE "F": ty$ = "Floating Pt"      CASE "D": ty$ = "Date"      CASE "M": ty$ = "Memo"      CASE ELSE: ty$ = "Unknown"   END SELECT   PRINT USING FieldString2$; ty$;_     FLDS(I).FdLength; FLDS(I).FdDecNEXT IPRINT "   ** Total **"; TAB(33);PRINT USING "####"; Hdr.RecordLength END SUB SUB Pause  PRINT  PRINT "Press any key to continue"  WHILE INKEY$ = "": WENDEND SUB SUB PrintDbfRecord (fv$(), RecNum)  '-------------------------------------------------'Purpose: Print the record to the screen.  Left  -'         justify character, date and logical    -'         fields.  Right justify numeric fields  -'         and ignore memo fields                 -'Input  : Field values store in character array, -'         current record number                  -'------------------------------------------------- ' Print rec # & delete statusColumnSpace = 4              'Room between columnsPRINT USING "####### !"; RecNum; fv$(0); ColumnLocation = 10          'Set current locationFOR I = 1 TO Hdr.NumberFields  IF FLDS(I).FdType <> "M" THEN    PRINT TAB(ColumnLocation);    IF FLDS(I).FdType = "N" OR   _       FLDS(I).FdType = "F" THEN      PRINT RightJust$(fv$(I), FLDS(I).FdLength);    ELSE      PRINT fv$(I);    END IF'       Set next print location    ColumnLocation = ColumnLocation +_       FLDS(I).FdLength + ColumnSpace  END IFNEXT IPRINT END SUB SUB PrintReport  '-------------------------------------------------'Purpose: Main printing routine                  -'Calls  : ReadDbfRecord                          -'         PrintDbfRecord                         -'------------------------------------------------- DIM FieldValues$(Hdr.NumberFields)PRINT : PRINTPRINT "Report on the "; FileName$; " file"PRINTFOR I = 1 TO Hdr.NumberRecords   CALL ReadDbfRecord(FieldValues$())   CALL PrintDbfRecord(FieldValues$(), I)NEXT IEND SUB FUNCTION ReadDbfHdr '-------------------------------------------------'Purpose: Read the dBASE file header information -'         and store in the header record         -                           '------------------------------------------------- HdrStr$ = SPACE$(32)GET #1, , HdrStr$               'Read dBASE Header Hdr.VersionNumber = ASC(LEFT$(HdrStr$, 1)) AND (7) UpdYY$ = ZeroJust$(ASC(MID$(HdrStr$, 2, 1)))UpdMM$ = ZeroJust$(ASC(MID$(HdrStr$, 3, 1)))UpdDD$ = ZeroJust$(ASC(MID$(HdrStr$, 4, 1))) Hdr.LastUpdate = UpdMM$+"/"+UpdDD$+"/"+UpdYY$ Hdr.NumberRecords = CVL(MID$(HdrStr$, 5, 4))Hdr.HeaderLength = CVI(MID$(HdrStr$, 9, 2))Hdr.RecordLength = CVI(MID$(HdrStr$, 11, 2)) Hdr.NumberFields = (Hdr.HeaderLength - 33) / 32Hdr.FileSize = Hdr.HeaderLength + Hdr.RecordLength_                          * Hdr.NumberRecords + 1 IF Hdr.VersionNumber <> 3 THEN   ReadDbfHdr = 1                'Not a dBASE file   EXIT FUNCTIONEND IF IF Hdr.NumberRecords = 0 THEN   ReadDbfHdr = 2                'No records   EXIT FUNCTIONEND IFReadDbfHdr = 0                   'No errorsEND FUNCTION SUB ReadDbfRecord (fv$())  '-------------------------------------------------'Purpose: Read a dBASE record, format date and   -'         logical fields for output              -'Input  : Array of Field values                  -'------------------------------------------------- F$ = SPACE$(Hdr.RecordLength)GET #1, , F$                      'Read the record fv$(0) = LEFT$(F$, 1)    'Read deleted record markFPOS = 2 FOR I = 1 TO Hdr.NumberFields    fv$(I) = MID$(F$, FPOS, FLDS(I).FdLength)    SELECT CASE FLDS(I).FdType  'Adjust field types      CASE "D"                 'Modify date format         y$ = LEFT$(fv$(I), 4)         M$ = MID$(fv$(I), 5, 2)         d$ = RIGHT$(fv$(I), 2)         fv$(I) = M$ + "/" + d$ + "/" + y$      CASE "L"                 'Standardize T or F          SELECT CASE UCASE$(fv$(I))             CASE "Y", "T": fv$(I) = ".T."             CASE "N", "F": fv$(I) = ".F."             CASE ELSE: fv$(I) = ".?."          END SELECT      CASE ELSE   END SELECT   FPOS = FPOS + FLDS(I).FdLength 'Set next fld'   PRINT fv$(I) NEXT IEND SUB FUNCTION ReadFileStructure  '-------------------------------------------------'Purpose: Read the file structure store in the   -'         dBASE file header.                     -'------------------------------------------------- FOR I = 1 TO Hdr.NumberFields   Fld$ = SPACE$(32)   GET #1, , Fld$           'Get field info string   FLDS(I).FdName = LEFT$(Fld$, 11)   FLDS(I).FdType = MID$(Fld$, 12, 1)   FLDS(I).FdLength = ASC(MID$(Fld$, 17, 1))   FLDS(I).FdDec = ASC(MID$(Fld$, 18, 1))NEXT IHeaderTerminator$ = INPUT$(1, #1)   'Last hdr byteIF ASC(HeaderTerminator$) <> 13 THEN   ReadFileStructure = False       'Bad Dbf headerEND IFReadFileStructure = TrueEND FUNCTION FUNCTION RightJust$ (Value$, FieldWidth)  '-------------------------------------------------'Purpose: Right justify a string by padding it   -'         with spaces on the left                -'Input  : The character value to justify, the    -'         width of the field to fit              -'Output : A right justified string to print      -'------------------------------------------------- RightJust$ = RIGHT$(STRING$(FieldWidth, " ") +_                          Value$, FieldWidth)END FUNCTION DEFSNG A-ZFUNCTION ZeroJust$ (Number AS INTEGER)  '-------------------------------------------------'Purpose: Add a leading zero to numbers less     -'         than 10 so they take as much room as   -'         numbers 10 and larger                  -'Input  : The number to standardize              -'Output : The adjusted number                    -'------------------------------------------------- N$ = STR$(Number)LengthN = LEN(N$) - 1'Subtract 1 for leading spaceN$ = RIGHT$("0" + RIGHT$(N$, LengthN), 2)ZeroJust$ = N$END FUNCTION

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -