Microsoft KB Archive/62057

= Basic Random Files Compatible with C Random (Binary) Files =

Article ID: 62057

Article Last Modified on 11/21/2006

-

APPLIES TO


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

-



This article was previously published under Q62057



SUMMARY
Basic random access files created with either user-defined TYPE records or with the FIELD statement can be easily read as C random access files. This article gives an example of writing two random access files using both a TYPEd record and a FIELD statement. An accompanying C program shows how to read those files as C random access files.

This information applies to Microsoft QuickBasic versions 4.00, 4.00b, and 4.50; to Microsoft Basic Compiler versions 6.00 and 6.00b; and to Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10 for MS-DOS and MS OS/2.



MORE INFORMATION
Versions of QuickBasic earlier than 4.00 use Microsoft Binary format (MBF) for floating-point numbers. Current versions of the C compiler expect floating-point numbers to be stored in IEEE format. Therefore, current versions of C cannot easily read random access files that are created with QuickBasic versions 1.00, 1.01, 1.02, 2.00, 2.01, and 3.00 if those files contain single- or double-precision numbers.

Both Basic and C store random access file records as byte-per-byte copies of the record variable written to the file. No &quot;extra&quot; information about the data is stored along with the record. A direct image of the record variable is copied to the record on disk. Therefore, because Basic and C have many data types in common, it is a straightforward process to create records in C that are the equivalent of Basic records. The following table summarizes what C data type you would use in a &quot;struct&quot; to mirror the data type in a Basic TYPE.   Basic TYPE Data Type   C struct Data Type --  INTEGER                int LONG                  long SINGLE                float DOUBLE                double STRING * n            char array[] The only precaution is that all strings passed from Basic to C in a TYPE should have a null character (CHR$(0)) as the last character if any C string-manipulation functions are going to be used.

The following Basic example shows writing both a TYPEd record and a FIELDed record to a Basic random access file. The following C program can then be used to read and display the contents of the two files created with Basic. The Basic program should be compiled with QuickBasic version 4.00, 4.00b, or 4.50, Microsoft Basic Compiler version 6.00 or 6.00b, or Basic PDS version 7.00 or 7.10. The C program should be compiled with C version 5.00 or 5.10, or with QuickC version 1.00, 1.01, 2.00, 2.01, 2.50, or 2.51.

Code Example
'****************************** Basic ***************************

TYPE rec1 s1 AS SINGLE d1 AS DOUBLE i1 AS INTEGER l1 AS LONG END TYPE

DIM var1 AS rec1 var1.s1 = 1.1: var1.d1 = 2.2: var1.i1 = 3: var1.l1 = 4

OPEN &quot;type.dat&quot; FOR RANDOM AS #1 PUT 1, 1, var1

OPEN &quot;field.dat&quot; FOR RANDOM AS #2 LEN = LEN(var1) FIELD #2, 4 AS single1$, 8 AS double1$, 2 AS int1$, 4 AS long1$ LSET single1$ = MKS$(var1.s1) LSET double1$ = MKD$(var1.d1) LSET int1$ = MKI$(var1.i1) LSET long1$ = MKL$(var1.l1) PUT 2, 1 CLOSE END

/************************************************************/ /*                           C Program                     */ /************************************************************/

typedef struct { float    float1; double   double1; int      int1; long int long1; } RECORD;
 * 1) include 

void printit(RECORD * rec1);

void main(void) {

FILE * TYPEdfile = NULL; FILE * FIELDfile = NULL; int NumRead; RECORD rec1;

TYPEdfile = fopen(&quot;TYPE.DAT&quot;,&quot;r&quot;); if(TYPEdfile == NULL) { printf(&quot;Open fail on single.dat\n&quot;); }  else { NumRead = fread(&rec1,sizeof(rec1),1,TYPEdfile); if (NumRead == 0) printf(&quot;Failed to read double.dat\n&quot;); printf(&quot;Contents of random file created with TYPE: \n&quot;); printit(&rec1); }

FIELDfile = fopen(&quot;FIELD.DAT&quot;,&quot;r&quot;); if(FIELDfile == NULL) { printf(&quot;Open fail on field.dat\n&quot;); }  else { printf(&quot;Contents of random file created with FIELD: \n&quot;); NumRead = fread(&rec1,sizeof(rec1),1,FIELDfile); printit(&rec1); }

fcloseall; }

void printit(RECORD * rec1) {  printf(&quot;%f\n&quot;,   rec1->float1); printf(&quot;%lf\n&quot;, rec1->double1); printf(&quot;%d\n&quot;,  rec1->int1); printf(&quot;%ld\n\n&quot;,rec1->long1); }

Additional query words: QuickBas BasicCom

Keywords: KB62057

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.