caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Yan Jun Daisy Chen <yjc01@doc.ic.ac.uk>
To: Karl Zilles <zilles@1969.ws>
Cc: caml-list@yquem.inria.fr
Subject: [Caml-list] read file
Date: Wed, 26 Jan 2005 14:41:41 +0000 (GMT)	[thread overview]
Message-ID: <Pine.LNX.4.58.0501261436020.12039@quantum06.doc.ic.ac.uk> (raw)
In-Reply-To: <41F6CB7B.7050406@1969.ws>

 I am trying to extract all the
comments i.e. all the text between (* and *) in a text file and store
them in a list of string. How do u determin the length of the file?

My idea is to read in the file_descr into a string variable, buff. Then
extract the comments recursively from buff. What would be the
appropriate string functions to do so? Or is there a simpler way to read
the comments in directly from file?

The code I come up with is as folllows:
open Unix;;

let fileReader = openfile "student.cd" [O_RDONLY] 0o640;;
let buff = ref "file content:	";;
(*let fileSize = (fstat fileReader).st_size;;*)
let fileSize = 10000000;;
let noOfChar = ref 0;;

let rec extract_comment () =
  let openIndex = 0 in
  noOfChar := read fileReader !buff openIndex fileSize;;


let main () =
  (*let fileContent = read fileReader !buff 0 5 in
  print_int fileContent;*)
  print_int !noOfChar;;
  print_newline();;
  print_string !buff;;
  print_newline();;


main ();;

Thanks

Daisy

 On Tue, 25 Jan 2005, Karl Zilles wrote:

>(forgot to copy the list, originally)
>
>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.
>
>
>My guess is that you aren't including the unix library on your compile
>command line:
>
>         ocamlc [other_options] unix.cma [other_files]
>         ocamlopt [other_options] unix.cmxa [other_files]
>
> >
> > The code I came up with is as follows:
> > open Unix;;
> >
> > let file_reader = openfile "sudent.cd" [O_RDONLY] 0o640;;
> > let buff = ref "empty";;
> >
> > let main () =
> >   let file_content = read file_reader !buff 1 5 in
>
>
>Strings are indexed from 0, so change above to
>let file_content = read file_reader !buff 0 5 in
>
>
> >   print_int file_content;
> >   print_string !buff;;
> > main ();
> > I couldn't work out what went wrong. Can anyone help?!!
>
>
>Also, using the Unix libray is probably overkill for this kind of thing.
>  You might want to switch to the Pervasives library, which is
>automatically included with all ocaml programs.
>
>There is a whole group for asking beginning OCaml questions:
>
>http://groups.yahoo.com/group/ocaml_beginners/
>
>


  parent reply	other threads:[~2005-01-26 14:41 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   ` Yan Jun Daisy Chen [this message]
2005-01-26 15:50     ` [Caml-list] read file 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

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=Pine.LNX.4.58.0501261436020.12039@quantum06.doc.ic.ac.uk \
    --to=yjc01@doc.ic.ac.uk \
    --cc=caml-list@yquem.inria.fr \
    --cc=zilles@1969.ws \
    /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).