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
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * Copyright (C) 2016 - Samuel GOUGEON
9 * This file is hereby licensed under the terms of the GNU GPL v2.0,
10 * pursuant to article 5.3.4 of the CeCILL v.2.1.
11 * This file was originally licensed under the terms of the CeCILL v2.1,
12 * and continues to be available under such terms.
13 * For more information, see the COPYING file which you should have received
14 * along with this program.
17 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
18 xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml"
19 xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
20 xmlns:scilab="http://www.scilab.org" xml:id="mget" xml:lang="en">
22 <refname>mget</refname>
24 parses numbers in a binary file and returns them as decimals
31 D = mget(nNumb, binFormat)
32 D = mget(nNumb, binFormat, fileID)
35 <refnamediv xml:id="mgeti">
36 <refname>mgeti</refname>
38 parses numbers in a binary file and returns them as encoded integers
45 I = mgeti(nNumb, binFormat)
46 I = mgeti(nNumb, binFormat, fileID)
50 <title>Arguments</title>
56 file identifier (single integer) returned by
57 <function>mopen</function> when opening the file.
58 By default, the last opened file is considered.
60 The file must be priorly opened in
61 <emphasis role="bold">r</emphasis>ead
62 <emphasis role="bold">b</emphasis>inary mode with
63 <code>fileID = mopen(filename,'rb')</code>.
72 Single integer > 0: number of numbers to be
73 parsed and returned. Each number is encoded over one or
74 several bytes, according to the used <varname>binFormat</varname>.
75 <note>To read all numbers remaining in the file, use a
76 value <varname>nNumb</varname> big enough.
82 <term>binFormat</term>
85 a text word made of one, 2 or 3 character codes: the binary
86 format used to parse numbers in the binary file.
87 Available binary codes are the following:
90 <td align="right">c</td>
91 <td>: individual bytes parsed as
92 <literal>int8</literal> integers
96 <td align="right">uc</td>
97 <td>: individual bytes parsed as
98 <literal>uint8</literal> unsigned integers >0
102 <td align="right">s</td>
103 <td>: 2-byte blocks parsed as
104 <literal>int16</literal> integers
108 <td align="right">us</td>
109 <td>: 2-byte blocks parsed as
110 <literal>uint16</literal> unsigned integers >0
114 <td align="right">i</td>
115 <td>: 4-byte blocks parsed as
116 <literal>int32</literal> integers
117 (<emphasis role="bold">default mode</emphasis>).
121 <td align="right">ui</td>
122 <td>: 4-byte blocks parsed as
123 <literal>uint32</literal> unsigned integers >0
127 <td align="right">l</td>
128 <td>: 8-byte blocks parsed as
129 <literal>int64</literal> integers
133 <td align="right">ul</td>
134 <td>: 8-byte blocks parsed as
135 <literal>uint64</literal> unsigned integers >0
141 Only with <function>mget()</function> :
145 <td align="right">f</td>
146 <td>: 4-byte blocks parsed as
147 "single precision" decimal numbers
148 (so-called "<emphasis role="bold">f</emphasis>loats"
153 <td align="right">d</td>
154 <td>: 8-byte blocks parsed as
155 <emphasis role="bold">d</emphasis>ecimal numbers
161 The default order of bytes in a block can be set using
162 a <function>mopen</function> option when opening the file.
163 This order may be forced afterwards using an optional
164 <function>mget</function> or <function>mgeti</function>
165 flag to be appended to <varname>binFormat</varname>:
168 <td align="right">..l :</td>
169 <td><emphasis role="bold">l</emphasis>ittle endian
170 (first byte in the block = low power byte)
174 <td align="right">..b :</td>
175 <td><emphasis role="bold">b</emphasis>ig endian
176 (first byte in the block = high power byte)
187 Row of <varname>nNumb</varname> Decimal numbers
188 (or available ones if the End Of File has been reached).
196 Row of <varname>nNumb</varname> encoded Integers
197 (or available ones if the End Of File has been reached).
198 The <function>inttype</function> of returned integers
199 depends on the used <varname>binFormat</varname>.
206 <title>Description</title>
208 <function>mget</function> and <function>mgeti</function> start reading
209 bytes in the specified file from the current position of the inner
210 file pointer. After reading a block of N bytes (N==1,2,4,8 according
211 to the chosen <varname>binFormat</varname>),
214 the file pointer is increased by N bytes and set to the
215 beginning of next block.
218 the read block is ordered according to the default or chosen
219 swapping endian mode. The parsed block is then stored to
223 This is iterated <varname>nNumb</varname> times, unless
224 the End Of File is reached: In this case, parsing the file is
225 stopped, the uncomplete block is left (if any), the EOF status is set,
226 and already parsed numbers are returned.
229 When <function>mgeti()</function> is used, parsed numbers are
230 converted into the <function>inttype</function> corresponding to
231 the chosen <varname>binFormat</varname> and then returned.
234 When <function>mget()</function> is used, binary numbers are
235 parsed according to <varname>binFormat</varname> but are finally
236 converted into 8-byte decimal numbers and then returned.
238 If <literal>int64</literal> or <literal>uint64</literal>
239 integers > 2<superscript>53</superscript> were parsed thanks
240 to the <literal>"ul*"</literal> or <literal>"l*"</literal> format,
241 their final conversion into decimal numbers
242 truncates their mantissa to their 53 highest bits.
247 <title>Examples</title>
248 <programlisting role="example"><![CDATA[
249 binfile = TMPDIR+"/mgetest.bin";
250 idF = mopen(binfile, "w+b");
251 mput(int8(0:16),"uc");
254 mgeti(1,"uc") // 0 expected
255 mgeti(2,"uc") // 1, 2 expected
256 [mgeti(1,"us"), uint16(3 + 4*256)]
257 mseek(3); // back to the previous position on "3"
258 [mgeti(1,"usb"), uint16(4 + 3*256)] // swapped bytes (big endian)
260 [mgeti(1,"ui") , uint32(0 + 256*(1 + 256*(2 + 256*3)))]
262 [mgeti(1,"uib"), uint32(3 + 256*(2 + 256*(1 + 256*0)))]
265 // uint64 and int64 integers with a relative accuracy of 1/2^64 = %eps/2^12
266 // better than decimals one are well handled:
267 // Generating n 64-bit long integers with bits #0-#63 set randomly:
269 b = grand(64,n,"uin",0,1);
270 p = uint64(2).^ndgrid(0:63,1:n);
273 // We write them in a file, and then re-read them with mgeti():
275 for endian = ["l" "b"]
276 binfile = TMPDIR+"/mgetiTestInt64.dat";
277 idF = mopen(binfile, "w+b");
282 mput(x,usign+"l"+endian) // "l" is mandatory to manage all the 64 bits
284 // Now, read them in the same mode:
286 xr = mgeti(n, usign+"l"+endian);
290 wrParse = usign + "l" + endian;
291 printf(" Write as ""%s"" Read as ""%s""\n", wrParse, wrParse);
297 <refsection role="see also">
298 <title>See also</title>
299 <simplelist type="inline">
301 <link linkend="mopen">mopen</link>
304 <link linkend="mclose">mclose</link>
307 <link linkend="mput">mput</link>
310 <link linkend="mseek">mseek</link>
313 <link linkend="mtell">mtell</link>
316 <link linkend="meof">meof</link>
319 <link linkend="readb">readb</link>
322 <link linkend="read4b">read4b</link>
325 <link linkend="inttype">inttype</link>
329 <refsection role="history">
330 <title>History</title>
333 <revnumber>6.1.0</revnumber>
334 <revdescription>mgeti(,"ul*"|"l*") is implemented
335 to read uint64 or int64 integers > 2<superscript>52</superscript>.