<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi Volker</p>
<blockquote type="cite"
cite="mid:9e86fd24e8505d3644ad14cba311bbf7fbb786f3.camel@volker-wysk.de"><br>
<pre class="moz-quote-pre" wrap="">I want to read the contents of a file, using open_input,
read_file_as_string, close_input. In case the file doesn't exist, I want to
return an empty string. So, in case of an error, I want to determine if it's
because the file doesn't exist.
It isn't a show stopper to me, I can return "" in case of any error, but it
isn't optimal.
Cheers,
Volker
</pre>
</blockquote>
<p>Why do you need to test according to io.error? <br>
</p>
<p>io.error will only "materialize" logical failure. Which takes
place further up in the tree.</p>
<p>So you just need to test against open_input out-mode argument
values.<br>
</p>
<p>Have you tried something along these lines?</p>
<p><br>
</p>
<p>:- pred my_pred(string::in, string::out, io::di, io::uo) is det.<br>
</p>
<p>my_pred(InputFileName, ResultStr, !IO) :- <br>
</p>
<p> (...),</p>
<p> io.stderr_stream(StdErrStream, !IO), <br>
</p>
<p> io.open_input(InputFileName, OpenInputResult,
!IO),
<br>
(
<br>
OpenInputResult =
ok(InputStream),
<br>
io.read_file_as_string(InputStream, ReadInputResult,
!IO),
<br>
(
<br>
ReadInputResult =
ok(InputFileString),
<br>
</p>
<p> (... unify ResultStr...)<br>
</p>
<p>
;
<br>
ReadInputResult = error(_,
ReadInputError),
<br>
io.error_message(ReadInputError,
ReadInputErrorMsg),
<br>
io.format(StdErrStream, "error reading %s:
%s\n",
<br>
[s(InputFileName), s(ReadInputErrorMsg)],
!IO),
<br>
io.set_exit_status(1, !IO) % or specific value
for ResultStr.
<br>
)
<br>
;
<br>
OpenInputResult =
error(OpenInputError),
<br>
io.error_message(OpenInputError,
OpenInputErrorMsg),
<br>
io.format(StdErrStream, "error opening %s:
%s\n",
<br>
[s(InputFileName), s(OpenInputErrorMsg)],
!IO),
<br>
ResultStr="" <br>
) <br>
</p>
<p><br>
<span style="font-family:monospace"><span
style="font-family:monospace"><span
style="color:#18b218;background-color:#ffffff;"></span><br>
</span></span></p>
</body>
</html>