From: Oliver Bandel <oliver@first.in-berlin.de>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] open_file
Date: Tue, 25 Jan 2005 23:54:49 +0100 [thread overview]
Message-ID: <20050125225449.GB667@first.in-berlin.de> (raw)
In-Reply-To: <1106691570.41f6c5f250fdc@www.doc.ic.ac.uk>
On Tue, Jan 25, 2005 at 10:19:30PM +0000, yjc01@doc.ic.ac.uk wrote:
> I tried to read in a text file and extract the string between (* and *) into a
> variable, buff, by using the openfile and read functions in Unix module. But
> get an error on compilation.
>
> The code I came up with is as follows:
>
> open Unix;;
>
> let file_reader = openfile "sudent.cd" [O_RDONLY] 0o640;;
assuming the file "sudent.cd" exists and contains enough bytes.
> let buff = ref "empty";;
>
> let main () =
> let file_content = read file_reader !buff 1 5 in
> print_int file_content;
> print_string !buff;;
> main ();
assuming you meant
main ();;
in the last line,....
then you have a problem, when you read from position with index "1" (instead of "0"
for the fiorst byte) and read 5 bytes into the 5 byte long string....
There is not enough space in your buffer for writing all your data into it.
You may start writing at position 0 instead of 1 or only read in 4 bytes, or
make your buffer larger.
You may set the len of bytes to the size of bytes of your String
(String.length !buff) . But you have to write from position (offset) 0.
It would be better to use the try/with
construct around your unix-calls; then you can handle exceptions.
E.g. when there is no file "sudent.cd" then your program fails
in your first line.
Ciao,
Oliver
prev parent reply other threads:[~2005-01-25 22:54 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-01-25 22:19 open_file yjc01
2005-01-25 22:43 ` [Caml-list] open_file Karl Zilles
2005-01-25 22:58 ` Oliver Bandel
2005-01-26 14:41 ` [Caml-list] read file Yan Jun Daisy Chen
2005-01-26 15:50 ` Oliver Bandel
2005-01-25 22:54 ` [Caml-list] open_file Karl Zilles
2005-01-26 10:16 ` Marcin 'Qrczak' Kowalczyk
2005-01-25 22:54 ` Oliver Bandel [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20050125225449.GB667@first.in-berlin.de \
--to=oliver@first.in-berlin.de \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).