[mercury-users] Input Output

Ondrej Bojar oboj7042 at ss1000.ms.mff.cuni.cz
Wed Oct 30 21:20:16 AEDT 2002


On 30 Oct 2002, Noel  Pinto wrote:
> I am trying to write a program that will create a new file if it
> does not exist and then start appending Information to it.

If io__open_append succeeds, it returns an output stream open. Use
io__set_output_stream to set this output stream. Then, all the predicates
io__write, io__print... will write to the given file, instead of stdout as
default. You could also use different version of the write predicates:

:- pred io__write(io__output_stream, T, io__state, io__state).
:- mode io__write(in, in, di, uo) is det.

Here, supply the output stream as the first argument. This way, you do not
need to io__set_output_stream.

Example:

main -->
  io_open_append("filename", Res),
  (
  { Res = error(E) },
     io__write_string("Error opening file: "),
     io__write(io__error_message(E))),
     io__nl
  ;
  { Res = ok(OutStream) },
     io__write_string(OutStream, "This goes at the end of the file.\n"),
     io__write_string("This goes to stdout.\n"),
     io__close_output(OutStream)
  ).

Maybe you were trapped at the first type definition of the type io__res/0:

:- type io__res         --->    ok
                        ;       error(io__error).

Please note, that there is also a different type definition, io__res/1:

:- type io__res(T)      --->    ok(T)
                        ;       error(io__error).

The predicate io__open_append uses the second one, and therefore returns
the output stream, when successful.

Andrew.

>
> What I understand is...
> 1) io__open_append  opens the file for appending and returns
> whether the file could be opened and if not then an error
>
> 2) io__read_line_as_string accepts a line from the user
>
> 3) io__set_input_stream changes the current input stream to the
> stream specified.
>
> I am at a loss, I mean that I am not able to understand how do I
> use these functions for what I want to do. If these are not the
> right functions then which to use??
>
> I am not able to find an example in the tests or samples directory
> for it. There is only for opening a file for viewing its contents.
> It does not provide any help for creating new files and appending
> information to it by the end user.
>
> I know I should be reading the io.m file for the solution. But, I
> am not able to understand how to use it for what I want.
>
> Plz do reply as to which directory I could get the example or plz
> do explain with a small example.
>
> Thanks and Regards,
>
> Noel
> --------------------------------------------------------------------------
> mercury-users mailing list
> post:  mercury-users at cs.mu.oz.au
> administrative address: owner-mercury-users at cs.mu.oz.au
> unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
> subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
> --------------------------------------------------------------------------
>

--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list