ocaml / ocaml

The core OCaml system: compilers, runtime system, base libraries
https://ocaml.org
Other
5.18k stars 1.06k forks source link

235 ^M are not read by really_input #2593

Closed vicuna closed 21 years ago

vicuna commented 23 years ago

Original bug ID: 208 Reporter: administrator Status: closed Resolution: not a bug Priority: normal Severity: minor Category: ~DO NOT USE (was: OCaml general)

Bug description

Full_Name: mattias waldau Version: 3.00 OS: windows 2000 Submission from: dialup-31.abc.se (195.17.73.31)

Using O'Caml 3.00, Windows 2000.

I am trying to read in a whole file into a string using the following predicate. Note that I have to remove 235 (probably since there are 235 ^M in the file), otherwise I get an end-of-file error. channel_length is doing correctly and returning 24961.

( 649 1800 24961 c:/stock/www.dljdirect.com/20000722151743.html )

let read_all2 filename = let ic = open_in filename in let size = in_channellength ic in let buf = String.create size in let = really_input ic buf 0 (size-235) in close_in ic; buf ;;

read_all2 "c:/stock/www.dljdirect.com/20000722151743.html";;

P.s. How should I attach the file?


Mattias Waldau, CTO Tacton AB, Saltmätargatan 7, 11359 Stockholm Mobile +46 70 549 11 12 http://www.tacton.com mailto:mattias.waldau@tacton.com

vicuna commented 23 years ago

Comment author: administrator

Full_Name: mattias waldau Version: 3.00 OS: windows 2000 Submission from: dialup-31.abc.se (195.17.73.31)

Using O'Caml 3.00, Windows 2000.

I am trying to read in a whole file into a string using the following predicate.

You mean function not predicate, do you ?

Note that I have to remove 235 (probably since there are 235 ^M in the file), otherwise I get an end-of-file error. channel_length is doing correctly and returning 24961.

( 649 1800 24961 c:/stock/www.dljdirect.com/20000722151743.html )

let read_all2 filename = let ic = open_in filename in let size = in_channellength ic in let buf = String.create size in let = really_input ic buf 0 (size-235) in close_in ic; buf ;;

read_all2 "c:/stock/www.dljdirect.com/20000722151743.html";;

P.s. How should I attach the file?


Mattias Waldau, CTO Tacton AB, Saltmätargatan 7, 11359 Stockholm Mobile +46 70 549 11 12 http://www.tacton.com mailto:mattias.waldau@tacton.com

Your problem is that you opened your file in text mode, instead of binary mode. Thus end-of-line markers are omitted from the input count.

You can use open_in_bin if you want to have the exact count of the characters written into the file. However this is not portable, since end-of-line markers are not the same under Unix, MacOS and Windows...

This suggest to add a function into the standard library to read a file into a string (or more generally a buffer). Thus, I would suggest the names buffer_of_file and string_of_file for such functions ...

Hope this helps,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/

vicuna commented 21 years ago

Comment author: administrator

User's misunderstanding of text mode vs. binary mode. Suggestion to add file reading functions to std lib. (See #2559)