March 28, 2024, 04:43:40 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


The Long and Short of String Files

Started by GWS, July 21, 2009, 08:33:33 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

Hi,

I'm writing some user notes and someone queried why this length difference occurs - does anyone have an answer?


OPENCONSOLE
def myfile: BFILE

' try this ..
def name1,name2:string
' and then this ..
'def name1[5],name2[5]:istring

' and then try changing the file type from Binary BFILE to just an ASCII FILE ..

' change the file to a suitable location for your own machine ..
IF(OPENFILE(myfile,"F:\filetest\CBASICTEST.TXT","W") = 0)
    name1 = "Sam"
    name2 = "Tom"
    WRITE myfile,name1
    WRITE myfile,name2
    CLOSEFILE myfile
    PRINT "File created successfully"
ELSE
    PRINT "File could not be created"
ENDIF

openfile(myfile,"F:\filetest\CBASICTEST.TXT","R")
print len(myfile)
closefile myfile

PRINT "Press Any Key To Close"

DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END



It appears that for binary files, ordinary 'string' variables write 255 byte records, however short the actual strings (but not for an ASCII file).

'istring' variables always write short records as expected.

all the best, :)

Graham
Tomorrow may be too late ..

ZeroDog

Im not surprised that the STRING takes up 255 bytes in a binary file, since thats the size of the variable itself.   And I'm not surprised that a string only takes up as much room as it needs in an ASCII file, since when you write a null to an ascii file, its basically like saying write nothing. 

I am surprised that an ISTRING doesnt write its full size into a binary file,  like the string does....

GWS

July 21, 2009, 10:22:17 AM #2 Last Edit: July 21, 2009, 12:07:26 PM by GWS
Ah!  . of course .. the 'Null' terminator ..  :)   that's how the ASCII file does it ..

So a binary file of strings will be longer 'cos it doesn't act on the content - only the length of the variable type.

Presumably, a UDT of ten strings would write a binary file 2550 bytes long, even if the content was only 'A','B','C','D','E','F','G','H','I','J'.

Yep, it does .. I just tried it ..  :)

And, an ASCII file with the same UDT only writes 11 bytes - the 'Null' characters doing the job again.

The moral seems to be - avoid a binary file of strings, use istrings instead (or use an ASCII datafile).

all the best, :)

Graham

Edit: Hmm .. I really must get round to reading the user guide a bit more ..  ::)

QuoteIn a binary file all data is written in raw form and
will occupy the full size of the dimensioned variable.


Tomorrow may be too late ..