Sunday, December 20, 2009

Size of a file

Rosetta Code is an interesting project where you can see code in different languages for the same problem. Its a wiki project and you are encouraged to put in your solution if one does not exist in your language of interest.

Here's a small bit contribution from me - Get the size of a file in scheme.

(define (file-size filename)
(call-with-input-file filename (lambda (port)
(let loop ((c (read-char port))
(count 0))
(if (eof-object? c)
count
(loop (read-char port) (+ 1 count)))))))

(file-size "input.txt")
(file-size "/input.txt")

No comments: