Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2024.0.0 - English


mgetstr

reads a character string from a file

Syntax

str = mgetstr(n, [fd])

Arguments

n

non-negative integer: how many characters should be read.

fd

an integer: file descriptor as returned by mopen: where characters should be read. The value -1 stands for the last opened file and is the default.

str

the character string to be read.

Description

The mgetstr function attempts to read up to n characters from a file. If end of file is reached before n characters are read, mgetstr returns the properly read values only. As a consequence, when the read cursor is already at the end of the file when mgetstr is called, it returns an empty string no matter what value is provided for n.

Examples

fn = SCI + '/ACKNOWLEDGEMENTS'; // absolute path to some file
details = fileinfo(fn);         // retrieve file details
len = details(1);               // get file length
fd = mopen(fn, 'rt');           // open file as text with read mode
str1 = mgetstr(33, fd)          // read 33 characters from fd
length(str1)                    // 33 characters read
str2 = mgetstr(272, fd)         // read the next 272 characters from fd
length(str2)                    // 272 characters read
mseek(len - 5);                 // jump to the 5th character before end of file
str3 = mgetstr(10, fd)          // try to read 10 characters: returns 5 characters only, no more available
length(str3)                    // yes, there are 5 characters: you just can't see the linefeed :)
str4 = mgetstr(10, fd)          // read 10 characters: returns empty string
length(str4)                    // empty string
mclose(fd);                     // close file descriptor

See also

  • mclose — closes opened files
  • 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
  • mopen — opens files in Scilab
  • mprintf — converts, formats, and writes data to the main scilab window
  • mput — writes byte or word in a given binary format
  • 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)
  • "Input/Output functions" section
Report an issue
<< mgetl Files : Input/Output functions mopen >>

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:
Tue Oct 24 14:30:04 CEST 2023