1 <?xml version="1.0" encoding="UTF-8"?>
3 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) 2008 - INRIA
5 * Copyright (C) 2011 - DIGITEO - Allan CORNET
7 * This file must be used under the terms of the CeCILL.
8 * This source file is licensed as described in the file COPYING, which
9 * you should have received as part of this distribution. The terms
10 * are also available at
11 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns4="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="mopen" xml:lang="en">
16 <refname>mopen</refname>
17 <refpurpose>opens a file in Scilab</refpurpose>
20 <title>Calling Sequence</title>
21 <synopsis>[fd, err] = mopen(file [, mode, swap ])</synopsis>
24 <title>Arguments</title>
29 <para>a character string. The pathname of the file to open.</para>
36 a character string that controls whether the file is opened for
37 reading (<literal>r</literal>), writing (<literal>w</literal>),
38 or appending (<literal>a</literal>) and whether the file is
39 opened for updating (<literal>+</literal>). The
40 <varname>mode</varname> can also include a <literal>b</literal>
41 parameter to indicate a binary file.
44 The default value is <literal>'rb'</literal>.
52 a scalar. If <varname>swap</varname> is present and
53 <code>swap = 0</code> then automatic bytes swap is
56 <para>The default value is 1.</para>
62 <para>a scalar. Error indicator.</para>
63 <informaltable border="1">
66 <td>error message:</td>
74 <td>no more logical units</td>
78 <td>cannot open file</td>
82 <td>no more memory</td>
90 <td>invalid status</td>
99 a scalar: a file descriptor (it's a positive integer).
106 <title>Description</title>
108 <function>mopen</function> may be used to open a <varname>file</varname> in a way
109 compatible with the C <function>fopen</function> procedure. Without
110 <varname>swap</varname> argument the <varname>file</varname> is supposed to be coded in "little endian IEEE
111 format" and data are swaped if necessary to match the IEEE format of
115 The <varname>mode</varname> parameter controls the access allowed to
116 the stream. The parameter can have one of the following values. In this
117 list of values, the <literal>b</literal> character indicates a binary
124 <para>opens the file for reading.</para>
130 <para>opens a binary file for reading.</para>
136 <para>opens a text file for reading.</para>
143 creates a new file for writing, or opens and truncates a file
152 creates a new binary file for writing, or opens and truncates
153 a file to zero length.
161 creates a text binary file for writing, or opens and truncates
162 a file to zero length.
170 appends (opens a file for writing at the end of the file, or
171 creates a file for writing).
176 <term>r+ or r+b</term>
178 <para>opens a file for update (reading and writing).</para>
182 <term>w+ or w+b</term>
184 <para>truncates to zero length or creates a file for update.</para>
188 <term>a+ or a+b</term>
191 appends (opens a file for update, writing at the end of the
192 file, or creates a file for writing).
198 When you open a file for update, you can perform both input and output
199 operations on the resulting stream. However, an output operation cannot
200 be directly followed by an input operation without a file-positioning
201 operation (<function>mseek</function> function). Also, an input
202 operation cannot be directly followed by an output operation without an
203 intervening file positioning operation, unless the input operation
204 encounters the end of the file.
207 When you open a file for append (that is, when the
208 <varname>mode</varname> parameter is <literal>a</literal> or
209 <literal>a+</literal>), it is impossible to overwrite information
210 already in the file. You can use the <function>mseek</function>
211 function to reposition the file pointer to any position in the file,
212 but when output is written to the file, the current file pointer is
213 ignored. All output is written at the end of the file and the file
214 pointer is repositioned to the end of the output.
217 To open files in a way compatible with Fortran-like functions use function <function>file</function>.
223 <title>Examples</title>
224 <programlisting role="example"><![CDATA[
225 // open a SCI+'/ACKNOWLEDGEMENTS' as text and read only
226 fd_r = mopen(SCI+'/ACKNOWLEDGEMENTS', 'rt')
228 // read five lines of fd_r
231 // another way to read file
232 // here read five words
233 mfscanf(5, fd_r, '%s')
235 // close file descriptor associated to SCI+'/ACKNOWLEDGEMENTS' as text and read only
242 <programlisting role="example"><![CDATA[
243 // open a file as text with write property
244 fd_w = mopen(TMPDIR+'/write.txt', 'wt');
246 // write a line in fd_w
247 mputl('This is a line of text', fd_w);
251 fd_r2 = mopen(TMPDIR+'/write.txt', 'rt');
259 <programlisting role="example"><![CDATA[
260 // read/write a file as binary
262 // first we write file
263 fd_wb = mopen(TMPDIR+'/writeread.bin', 'wb')
265 // put values as binary
266 mput(2003, 'l', fd_wb);
267 mput(2008, 'i', fd_wb);
268 mput(2012, 's', fd_wb);
269 mput(98, 'c', fd_wb);
271 // close file descriptor associated to TMPDIR+'/writeread.bin'
275 fd_rb = mopen(TMPDIR+'/writeread.bin', 'rb')
287 <refsection role="see also">
288 <title>See Also</title>
289 <simplelist type="inline">
291 <link linkend="file">file</link>
294 <link linkend="mclose">mclose</link>
297 <link linkend="merror">merror</link>
300 <link linkend="meof">meof</link>
303 <link linkend="mfprintf">mfprintf</link>
306 <link linkend="fprintfMat">fprintfMat</link>
309 <link linkend="mfscanf">mfscanf</link>
312 <link linkend="fscanfMat">fscanfMat</link>
315 <link linkend="mget">mget</link>
318 <link linkend="mgetl">mgetl</link>
321 <link linkend="mgetstr">mgetstr</link>
324 <link linkend="mprintf">mprintf</link>
327 <link linkend="mput">mput</link>
330 <link linkend="mputl">mputl</link>
333 <link linkend="mputstr">mputstr</link>
336 <link linkend="mseek">mseek</link>
339 <link linkend="mtell">mtell</link>
342 <link linkend="mdelete">mdelete</link>