mputstr
write a single text in an open file
Syntax
mputstr(str) mputstr(str, fid)
Arguments
- fid
integer: file identifier returned by
mopen(…)
.-1
(default) stands for the last opened file.- str
Single text to write in the file. Multilingual UTF-8 characters are accepted.
str
can include special formatting characters like ascii(9) (tab), ascii(10) (newline) and ascii(13) (carriage return). Sequences like "\t", "\n" and "\r" have no special meaning and are written as is.
Description
mputstr(…)
writes a character string str
in a binary or text file. str
is buffered: It is actually
written in the file only when closing this one.
Examples
myFile = tempname(); fid = mopen(myFile, "wt"); mputstr("Scilab", fid); mgetl(myFile) // => []. "Scilab" has been buffered mputstr(" and Xcos", fid); mgetl(myFile) // => still []. " and Xcos" added to the buffer // Only one text can be sent at a once: mputstr(["Science" ; "Innovation"], fid); // => error mclose(fid); mgetl(myFile) // => "Scilab and Xcos" mdelete(myFile);
--> mputstr("Scilab", fid); --> mgetl(myFile) // => []. "Scilab" has been buffered ans = [] --> mputstr(" and Xcos", fid); --> mgetl(myFile) // => still []. " and Xcos" added to the buffer ans = [] --> // Only one text can be sent: --> mputstr(["Science" ; "Innovation"], fid); // => error mputstr: Argument #1: Scalar (1 element) expected. --> mclose(fid); --> mgetl(myFile) ans = "Scilab and Xcos"
Use UTF-8 and special formatting characters:
[tab, nl] = (ascii(9), ascii(10)); myFile = tempname(); fid = mopen(myFile, "wt"); mputstr("Επιστήμη", fid); mputstr(ascii(10), fid); // Line feed mputstr("Τεχνολογία" + nl, fid); mputstr(tab, fid); // Heading tab mputstr("innovation", fid); mputstr(nl + "Science" + nl + tab + "technology", fid); mclose(fid); mgetl(myFile) mdelete(myFile);
--> mputstr("Επιστήμη", fid); --> mputstr(ascii(10), fid); // Line feed --> mputstr("Τεχνολογία" + nl, fid); --> mputstr(tab, fid); // Heading tab --> mputstr("innovation", fid); --> mputstr(nl + "Science" + nl + tab + "technology", fid); --> mclose(fid); --> mgetl(myFile) ans = "Επιστήμη" "Τεχνολογία" " innovation" "Science" " technology"
See also
- mgetstr — reads a character string from a file
- mputl — writes strings in a text file
- mgetl — reads lines from an text file
- mput — writes byte or word in a given binary format
- mfprintf — converts, formats, and writes data to a file
- save — Saves some chosen variables in a binary data file
- write — writes real numbers or strings in the console or in a formatted file
- csvWrite — Write comma-separated value file
Report an issue | ||
<< mputl | Files : Input/Output functions | mseek >> |