Browse thread
open_file
-
yjc01@d...
-
Karl Zilles
- Oliver Bandel
- Yan Jun Daisy Chen
- Karl Zilles
- Oliver Bandel
-
Karl Zilles
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2005-01-26 (14:41) |
From: | Yan Jun Daisy Chen <yjc01@d...> |
Subject: | [Caml-list] read file |
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/ > >