1

Hi I am trying to find the the 40th byte from a binary file using scheme commands.

(define FileName "D:/work_dsi/Stories/WriterOptions/Tests/block_english.model")

(define fPtr (open-input-file FileName))

(define sRec "1")

(((sRec (read-byte fPtr)))

    do
     ((eof-object? sRec ))
    (print sRec)
    (define sRec (read-byte fPtr))
)
(close-input-port fPtr)

i tried with this expression for a better understanding of the concept , but couldn't achieve anything.

1
  • It looks like you need to study the most fundamental basics of Scheme a bit more.
    – molbdnilo
    Commented Mar 11, 2021 at 14:55

2 Answers 2

2

You need these functions:

(require racket/port)

(with-input-from-file
    "D:/work_dsi/Stories/WriterOptions/Tests/block_english.model"
  (lambda ()
    (bytes-ref (port->bytes) 39)))

This function will return number. For reading text files, you can convert that number with integer->char to get character.

2
  • May i know what this is for ? (string->path "C:\\Users\\mapud\\Documents\\bytefile.txt") cause i already have defined a binary file for the function.
    – Umesh V
    Commented Mar 11, 2021 at 11:55
  • @UmeshV Actually, you don't need that- check edited version. Commented Mar 11, 2021 at 12:12
0

A version using only standard R7RS functions (In particular, open-binary-input-file and read-u8 from the file library:

(import (scheme file))
(define (read-nth-byte filename n)
  (call-with-port
   (open-binary-input-file filename)
   (lambda (inp)
     (do ((i 1 (+ i 1))
          (byte (read-u8 inp) (read-u8 inp)))
         ((= i n) byte)
       (if (eof-object? byte) (error "Premature end of file"))))))

To avoid reading a large chunk of the file into memory for large values of n, this one reads and discards bytes until it hits the nth byte. (It would be nice if there was a seek function in the standard...)

4
  • If i replace the n with 40 in the above script i am getting this output ! Error lambda: wrong argument type integer (expected symbol)
    – Umesh V
    Commented Mar 16, 2021 at 12:25
  • Also for the first line command : Error top-level: unbound variable: import
    – Umesh V
    Commented Mar 16, 2021 at 12:28
  • @UmeshV Er... are you replacing the variable in the argument list with a literal number? Um. It's a function; just call it with the appropriate arguments. This code requires a R7RS-compliant scheme. If you're using one that doesn't support that standard, it might not work (For example, if using chicken, you'd need to install the r7rs egg first).
    – Shawn
    Commented Mar 16, 2021 at 12:34
  • hi shawn instead of reading a byte , what i have done is imported the binary file and read the characters. But i had one issue in that i was not able to get the result for this script with a string match. Kindly check this link : stackoverflow.com/questions/66650490/… .
    – Umesh V
    Commented Mar 16, 2021 at 12:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.