PDA

View Full Version : Reading Bytes from a file



mendiburu
07-30-2004, 12:39 PM
Hello.
I have a dream....
I want to read a file who contain floats, coded on 4 bytes.
There's no way to read a float, right ?

Then I decided to code my hown 4-Bytes-IEEE-754-Float to LW-Scripts-Number.

It's extremely stupid, i agree, and I'll be so happy to learn a better solution.

I'm curently stuck in the first step : just reading the bytes from the file.

When I read any 'control' char, for instance Hex1A aka "substitute" the mess start and all the forecoming bytes are corrupted.

The number(readbyte()) is refused with a "parameter type error)"

And if i do fancy stuff like comparaison (if mychar==23) or computation (mychar=mychar+128) it just crashes Modeler....

Any clue, solution, advice ?

Maybe I just should use the SDK and make some real C/C++

Thanks.

Lynx3d
07-31-2004, 03:21 AM
Err...in what mode did you open the file, binary or ASCII?


readByte()
In ASCII mode, the readByte()function will return a single character from the text file as an integer value. In Binary mode, a single binary value of size unsigned char is read from the file and returned as an integer value.

Although it says that in both cases it should be an "integer", i guess binary shouldn't care what character that might be. But i haven't tried that. I only wonder how you'd convert that to a float in LScript (why does LScript only read doubles anyway?)...dissecting bits must be fun...
And don't forget, there's little and big endian machines, if written from another program it may be a problem.

So yea, maybe you should use C...then you can just directly write the 4 bytes into one float and you're done.

mendiburu
08-02-2004, 01:23 PM
I'd already found the "binary mode" point.
But there's *no* clue on how to open a file in one mode or another.

I'm still hunting the bug, but it looks like reading a Hex1A char closes the file...
And displaying it, using
info(string("my char is ",mychar));
let LW to repeat the previous string
>> my char is my char is
Until now, i've found none other char generating this issue.

Effectively, I'm dissecting bits to re-generate a float out of 4 bytes. It's a memory dive in my computer hardware courses, now almost 15 years old...

I got the sign and the exponent rights, still some trouble to convert the lower bytes of the signifiant.

And the all-types-fit-one engine behing the LScript gave me hard time.

Sure, I may use C. But I'd rather avoid external compilation and linkings issues. I've never wrote sucha plugin.
Is there any well documented HowTo I may use ?

Lynx3d
08-02-2004, 02:51 PM
Originally posted by mendiburu
I'd already found the "binary mode" point.
But there's *no* clue on how to open a file in one mode or another.

LScript reference manual, page 13:

File management
The file management commands handle files and directories.
File
The File() constructor accepts a string that points to the
location of a disk file, and an optional mode string that states how
to open the file. Then the file command returns a File Object Agent.
Please see page 129 in Volume 2, Chapter 13 File Object Agents for
an explanation of these Object Agents and their attributes.
sceneFile = File(“/temp.lws”,”w” );
The following list contains the File open modes, which are
identical to those provided by C:
r read (ascii)
w write (ascii)
a append (ascii)
b binary (i.e., “rb” would be read-binary)
+ with update (i.e., the file pointer can be moved
arbitrarily)
For example, to open a file for random binary read access, you
specify “rb+” as the open mode for your file.
If you do not specify the optional open mode, then the file is
opened in read-ascii (r) mode by default.

Also, in the page above:

ascii
The ascii() conversion function takes a character value and
returns its integer equivalent. This is handy for comparing nonprinting
characters.
if(ascii(line[x]) == ‘\t’ )

Perhaps that helps some...

mendiburu
08-04-2004, 02:48 PM
YES !!!! you saved my week !!!
One morer RTFM post....

Unfortunately, i'm using the online help who's far from being exaustive.

Is there any electronic version of the user manual I could get somewhere ?

I was curently fighting with file size limitations in the offset() method...

I did try some blind 'rb' combinations a few days ago, and it didn't make it.

Now, I got the solution with a reopen("rb+") after reading the magic number and clera-text headers of my file.

It really looks like the binary mode if much more powerfull than the ascii.

Opening the same file may need 5 seconds (text) or instant (binary)

Thanks folks, anf if you need to read float files in LScript, just ask for my code, it works fine now....

See U soon for my next problem.
Thanks

Lynx3d
08-05-2004, 02:17 AM
I do have the pdf versions ;)

See here at the bottom:
http://www.newtek.com/products/lightwave/downloads/index.html
But the "LScript 2.6 Updates" link is dead, perhaps it's somewhere on the ftp...