Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2024.1.0 - Português


mopen

opens files in Scilab

Syntax

[fd, err] = mopen(files [, mode, swap ])

Arguments

files

a matrix of strings containing the paths of the files to open.

mode

a string specifying the access mode requested for the files.

swap

a scalar. If swap is present and swap = 0 then automatic bytes swap is disabled.

The default value is 1.

err

a matrix of integers. Error indicators.

error value: error message:
0 No error
-1 no more logical units
-2 cannot open file
-3 no more memory
-4 invalid name
-5 invalid status
When this output argument is given, no error will be raised by the function.
fd

a matrix of file descriptors (they are positive integers).

Description

mopen may be used to open files in a way compatible with the C fopen procedure. Without swap argument the files are supposed to be coded in "little endian IEEE format" and data are swapped if necessary to match the IEEE format of the processor.

The mode parameter controls the access type requested for the stream. The parameter can have one of the following values:

r

opens for reading (default). The file must exist, otherwise it fails.

w

opens for writing. If the file exists, its contents are destroyed.

a

opens for appending. It creates the file if it does not exist.

r+

opens for both reading and writing. The file must exist, otherwise it fails.

w+

opens for both reading and writing. If the file exists, its contents are destroyed.

a+

opens for both reading and appending. It creates the file if it does not exist.

In addition, the following characters can be used to specify the type of file:

t

text file.

b

binary file (default).

The default access mode is 'rb' (binary file reading).

Note: on Windows, in text file mode, CR (carriage return) - LF (linefeed) combinations are translated into LF on reading, and LF are translated to CR-LF combinations on writing.

When you open a file for update, you can perform both input and output operations on the resulting stream. However, an output operation cannot be directly followed by an input operation without a file-positioning operation (mseek function). Also, an input operation cannot be directly followed by an output operation without an intervening file positioning operation, unless the input operation encounters the end of the file.

When you open a file for append (that is, when the mode parameter is a or a+), it is impossible to overwrite information already in the file. You can use the mseek function to reposition the file pointer to any position in the file, but when output is written to the file, the current file pointer is ignored. All output is written at the end of the file and the file pointer is repositioned to the end of the output.

To open files in a way compatible with Fortran-like functions use function file.

Examples

// open a SCI+'/ACKNOWLEDGEMENTS' as text and read only
fd_r = mopen(SCI+'/ACKNOWLEDGEMENTS', 'rt')

// read five lines of fd_r
mgetl(fd_r, 5)

// another way to read file
// here read five words
mfscanf(5, fd_r, '%s')

// close file descriptor associated to SCI+'/ACKNOWLEDGEMENTS' as text and read only
mclose(fd_r);
// open a file as text with write property
fd_w = mopen(TMPDIR+'/write.txt', 'wt');

// write a line in fd_w
mputl('This is a line of text', fd_w);
mclose(fd_w);

// read text
fd_r2 = mopen(TMPDIR+'/write.txt', 'rt');
mgetl(fd_r2)
mclose(fd_r2);
// read/write a file as binary

// first we write file
fd_wb = mopen(TMPDIR+'/writeread.bin', 'wb')

// put values as binary
mput(2003, 'l', fd_wb);
mput(2008, 'i', fd_wb);
mput(2012, 's', fd_wb);
mput(98, 'c', fd_wb);

// close file descriptor associated to TMPDIR+'/writeread.bin'
mclose(fd_wb);

// we read file
fd_rb = mopen(TMPDIR+'/writeread.bin', 'rb')

mget(fd_rb, 'l')
mget(fd_rb, 'i')
mget(fd_rb, 's')
mget(fd_rb, 'c')

mclose(fd_rb)

See also

  • file — Gerenciamento de arquivos
  • mclose — closes opened files
  • merror — tests the file access errors indicator
  • meof — checks if end of file has been reached
  • mfprintf — converts, formats, and writes data to a file
  • fprintfMat — writes a matrix in a file
  • mfscanf — reads input from the stream pointer stream (interface to the C fscanf function)
  • fscanfMat — reads a matrix from a text file
  • mget — parses numbers in a binary file and returns them as decimals
  • mgetl — reads lines from an text file
  • mgetstr — reads a character string from a file
  • mprintf — converts, formats, and writes data to the main scilab window
  • mput — writes byte or word in a given binary format
  • mputl — writes strings in a text file
  • mputstr — write a single text in an open file
  • mseek — sets current position in a binary file
  • mtell — Returns the offset of the current byte relative to the beginning of a file
  • mdelete — deletes file(s)
Report an issue
<< mgetstr Arquivos : funções de Entrada/Saída mput >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Jun 17 17:53:24 CEST 2024