Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Read data binary mode .......
Pages: [1] Print
Author Topic: Read data binary mode .......  (Read 925 times)
jfdutcher
Jr. Member
**
Offline Offline

Posts: 95


View Profile
Read data binary mode .......
« on: February 13, 2006, 06:20:41 PM »

An earlier post requesting input on reading files of fixed-length records layed end-to-end (no newlines or carriage-returns)  resulted in the code sample below which worked
beautifully right out of the box.
The two commented lines in the script show the data  as 'read' from
such a file.....and the data as 'printed' with  Rebol's print
function.
Can anyone straighten me out as to why the output of 'print' is not the 'textual' characters I probably expected.
Is it because :
1)  I don't want a 'binary' mode read even with this file
type.....OR   2)  I need to parameterize 'print' so as to de-code the data into what I expected (a match to what was
read,   i.e.  the data in the 1st comment)  Huh


Code:
Rebol[]

port: open/binary/seek %/c/vetssrc/dietsys/dsysmst.txt

read-record: func [port record-size record-number] [
  copy/part at port record-number - 1 * record-size + 1 record-size
]
substr: func [record offset len] [
  copy/part at record offset len
]

rec1: read-record port 2690 1

field: substr rec1 0 15       ;0015342HORNBECK
print field       ;#{30303135333432484F524E4245434B}
halt

Logged
CarlRead
Full Member
***
Offline Offline

Posts: 105


View Profile
Read data binary mode .......
« Reply #1 on: February 13, 2006, 10:14:17 PM »

It's because a string and a binary are different types in REBOL and what you're getting is the format that's been chosen to represent binary data, both within a script and when printed.  ie...

>> str: "abc"
== "abc"
>> type? str
== string!
>> bin: #{30303135333432484F524E4245434B}
== #{30303135333432484F524E4245434B}
>> type? bin
== binary!
>> print str
abc
>> print bin
#{30303135333432484F524E4245434B}

To get the result you expected, convert it to a string...

>> print to-string bin
0015342HORNBECK

Hope that helps.
Logged

- Carl Read
Guest_jfdutcher
Guest


Email
Read data binary mode .......
« Reply #2 on: February 14, 2006, 06:18:44 AM »

That makes things very clear.

Would you judge there was no need for me to open a port and do a binary read to process the file in question ?

Just because it has no newlines or carriage returns and needs to be thought of as fixed-length, adjacent, logical records.......maybe doesn't mean I have to use binary mode since  I think of and expect to
handle their content as 'text' (strings) ??

It's just that any other language I used to operate on these files (PHP, Basic, Python) required them to be opened as
binary mode.

J.D.
Logged
CarlRead
Full Member
***
Offline Offline

Posts: 105


View Profile
Read data binary mode .......
« Reply #3 on: February 14, 2006, 07:10:23 AM »

If the files aren't too long and there really are just letters and numbers in it, just reading the file should be fine.  (I'm not sure what would happen with very large files.)

But for safety, you could use...

file: to-string read/binary %file.txt

No need to open a port just to read binary files.
Logged

- Carl Read
Pages: [1] Print 
Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Read data binary mode .......
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 03, 2008, 09:49:33 PM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2315 Posts in 597 Topics by 4204 Members
Latest Member: hledamka.com

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc