caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* open_file
@ 2005-01-25 22:19 yjc01
  2005-01-25 22:43 ` [Caml-list] open_file Karl Zilles
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: yjc01 @ 2005-01-25 22:19 UTC (permalink / raw)
  To: caml-list

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;;
let buff = ref "empty";;

let main () =
  let file_content = read file_reader !buff 1 5 in
  print_int file_content;
  print_string !buff;;
main ();
I couldn't work out what went wrong. Can anyone help?!!

Daisy


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] open_file
  2005-01-25 22:19 open_file yjc01
@ 2005-01-25 22:43 ` Karl Zilles
  2005-01-25 22:58   ` Oliver Bandel
  2005-01-26 14:41   ` [Caml-list] read file Yan Jun Daisy Chen
  2005-01-25 22:54 ` [Caml-list] open_file Karl Zilles
  2005-01-25 22:54 ` Oliver Bandel
  2 siblings, 2 replies; 8+ messages in thread
From: Karl Zilles @ 2005-01-25 22:43 UTC (permalink / raw)
  To: yjc01; +Cc: caml-list

(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/


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] open_file
  2005-01-25 22:19 open_file yjc01
  2005-01-25 22:43 ` [Caml-list] open_file Karl Zilles
@ 2005-01-25 22:54 ` Karl Zilles
  2005-01-26 10:16   ` Marcin 'Qrczak' Kowalczyk
  2005-01-25 22:54 ` Oliver Bandel
  2 siblings, 1 reply; 8+ messages in thread
From: Karl Zilles @ 2005-01-25 22:54 UTC (permalink / raw)
  To: yjc01; +Cc: caml-list


yjc01@doc.ic.ac.uk wrote:
> The code I came up with is as follows: 

I forgot to add, there is no reason to use a reference for buff:

> 
> open Unix;;
> 
> let file_reader = openfile "sudent.cd" [O_RDONLY] 0o640;;
> let buff = ref "empty";;

let buff = "empty";;

> 
> let main () =
>   let file_content = read file_reader !buff 1 5 in

let file_content = read file_reader buff 0 5 in

>   print_int file_content;
>   print_string !buff;;

print_string buff;;

> main ();

This is because you are actually writing directly on the string, so 
there is no need for indirection.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] open_file
  2005-01-25 22:19 open_file yjc01
  2005-01-25 22:43 ` [Caml-list] open_file Karl Zilles
  2005-01-25 22:54 ` [Caml-list] open_file Karl Zilles
@ 2005-01-25 22:54 ` Oliver Bandel
  2 siblings, 0 replies; 8+ messages in thread
From: Oliver Bandel @ 2005-01-25 22:54 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] open_file
  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
  1 sibling, 0 replies; 8+ messages in thread
From: Oliver Bandel @ 2005-01-25 22:58 UTC (permalink / raw)
  To: caml-list

On Tue, Jan 25, 2005 at 02:43:07PM -0800, 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]
> 


OH,awell, the error was during compilation?
Well, I have overlooked this.


It's *always* good to have more informations on the kind of errors.

When the compilation-errors are gone, I'm sure there will be many
errors possivle during execution....



[...]
> >   print_int file_content;
> >   print_string !buff;;
> > main ();
> > I couldn't work out what went wrong. Can anyone help?!!

It couldn't work, because there is only one ";" at the last line...


> 
> Also, using the Unix libray is probably overkill for this kind of thing. 

Seems so.

>  You might want to switch to the Pervasives library, which is 
> automatically included with all ocaml programs.

Yes - that's a good high-level handling of I/O. It's buffered
and a quite good thing! :)

Ciao,
   Oliver


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] open_file
  2005-01-25 22:54 ` [Caml-list] open_file Karl Zilles
@ 2005-01-26 10:16   ` Marcin 'Qrczak' Kowalczyk
  0 siblings, 0 replies; 8+ messages in thread
From: Marcin 'Qrczak' Kowalczyk @ 2005-01-26 10:16 UTC (permalink / raw)
  To: caml-list

Karl Zilles <zilles@1969.ws> writes:

> let buff = "empty";;
>
>> let main () =
>>   let file_content = read file_reader !buff 1 5 in
>
> let file_content = read file_reader buff 0 5 in

You should not modify string literals, because they are allocated
statically. Use String.create to make a fresh string.

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Caml-list] read file
  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
  2005-01-26 15:50     ` Oliver Bandel
  1 sibling, 1 reply; 8+ messages in thread
From: Yan Jun Daisy Chen @ 2005-01-26 14:41 UTC (permalink / raw)
  To: Karl Zilles; +Cc: caml-list

 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/
>
>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] read file
  2005-01-26 14:41   ` [Caml-list] read file Yan Jun Daisy Chen
@ 2005-01-26 15:50     ` Oliver Bandel
  0 siblings, 0 replies; 8+ messages in thread
From: Oliver Bandel @ 2005-01-26 15:50 UTC (permalink / raw)
  To: caml-list

On Wed, Jan 26, 2005 at 02:41:41PM +0000, Yan Jun Daisy Chen wrote:
>  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?

The length of the file?
Not necessary to know.
Or are the comments maybe as long as the file itself?

You better do using OCaml's built-in operations for reading files.

Look for open_in, read_line and so forth.

You also can use the Buffer-module to be much faster,
when appending strings together to a bigger string.

You can do such scanning with ocamllex. But it's also possible
to write your own scanner.

Do not use Unix-module for that stuff. It's portable to use the built-in
functions open_in and such stuff (like read_line and so forts), or the
Buffer-module.

Use Unix-module only, if you need it (e.g. reading from pipes or such stuff).


Ciao,
   Oliver


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-01-26 15:50 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 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).