50 #include <sys/types.h>
119 tmp_files.push (file);
125 while (! tmp_files.empty ())
127 std::string filename = tmp_files.top ();
129 gnulib::unlink (filename.c_str ());
146 size_t pos = mode.find (
'W');
148 if (pos != std::string::npos)
151 "fopen: treating mode \"W\" as equivalent to \"w\"");
155 pos = mode.find (
'R');
157 if (pos != std::string::npos)
160 "fopen: treating mode \"R\" as equivalent to \"r\"");
164 pos = mode.find (
'A');
166 if (pos != std::string::npos)
169 "fopen: treating mode \"A\" as equivalent to \"a\"");
173 pos = mode.find (
'z');
175 if (pos != std::string::npos)
177 #if defined (HAVE_ZLIB)
181 error (
"this version of Octave does not support gzipped files");
190 size_t bpos = mode.find (
'b');
191 size_t tpos = mode.find (
't');
193 if (bpos == std::string::npos && tpos == std::string::npos)
199 static std::ios::openmode
202 std::ios::openmode retval = std::ios::in;
207 retval = std::ios::in;
208 else if (mode ==
"wt")
209 retval = std::ios::out | std::ios::trunc;
210 else if (mode ==
"at")
211 retval = std::ios::out | std::ios::app;
212 else if (mode ==
"r+t" || mode ==
"rt+")
213 retval = std::ios::in | std::ios::out;
214 else if (mode ==
"w+t" || mode ==
"wt+")
215 retval = std::ios::in | std::ios::out | std::ios::trunc;
216 else if (mode ==
"a+t" || mode ==
"at+")
217 retval = std::ios::in | std::ios::out | std::ios::app;
218 else if (mode ==
"rb" || mode ==
"r")
219 retval = std::ios::in | std::ios::binary;
220 else if (mode ==
"wb" || mode ==
"w")
221 retval = std::ios::out | std::ios::trunc | std::ios::binary;
222 else if (mode ==
"ab" || mode ==
"a")
223 retval = std::ios::out | std::ios::app | std::ios::binary;
224 else if (mode ==
"r+b" || mode ==
"rb+" || mode ==
"r+")
225 retval = std::ios::in | std::ios::out | std::ios::binary;
226 else if (mode ==
"w+b" || mode ==
"wb+" || mode ==
"w+")
227 retval = (std::ios::in | std::ios::out | std::ios::trunc
229 else if (mode ==
"a+b" || mode ==
"ab+" || mode ==
"a+")
230 retval = (std::ios::in | std::ios::out | std::ios::app
233 ::error (
"invalid mode specified");
239 DEFUN (fclose, args, ,
241 @deftypefn {Built-in Function} {} fclose (@var{fid})\n\
242 @deftypefnx {Built-in Function} {} fclose (\"all\")\n\
243 Close the specified file. If successful, @code{fclose} returns 0,\n\
244 otherwise, it returns -1. The second form of the @code{fclose} call closes\n\
245 all open files except @code{stdout}, @code{stderr}, and @code{stdin}.\n\
246 @seealso{fopen, freport}\n\
251 int nargin = args.
length ();
261 DEFUN (fclear, args, ,
263 @deftypefn {Built-in Function} {} fclear (@var{fid})\n\
264 Clear the stream state for the specified file.\n\
270 int nargin = args.
length ();
287 DEFUN (fflush, args, ,
289 @deftypefn {Built-in Function} {} fflush (@var{fid})\n\
290 Flush output to @var{fid}. This is useful for ensuring that all\n\
291 pending output makes it to the screen before some other event occurs.\n\
292 For example, it is always a good idea to flush the standard output\n\
293 stream before calling @code{input}.\n\
295 @code{fflush} returns 0 on success and an OS dependent error value\n\
296 (@minus{}1 on Unix) on error.\n\
297 @seealso{fopen, fclose}\n\
302 int nargin = args.
length ();
321 retval = os.
flush ();
330 DEFUN (fgetl, args, ,
332 @deftypefn {Built-in Function} {@var{str} =} fgetl (@var{fid})\n\
333 @deftypefnx {Built-in Function} {@var{str} =} fgetl (@var{fid}, @var{len})\n\
334 Read characters from a file, stopping after a newline, or EOF,\n\
335 or @var{len} characters have been read. The characters read, excluding\n\
336 the possible trailing newline, are returned as a string.\n\
338 If @var{len} is omitted, @code{fgetl} reads until the next newline\n\
341 If there are no more characters to read, @code{fgetl} returns @minus{}1.\n\
343 To read a line and return the terminating newline see @code{fgets}.\n\
344 @seealso{fgets, fscanf, fread, fopen}\n\
347 static std::string who =
"fgetl";
354 int nargin = args.
length ();
356 if (nargin == 1 || nargin == 2)
366 std::string tmp = os.
getl (len_arg, err, who);
370 retval(1) = tmp.
length ();
381 DEFUN (fgets, args, ,
383 @deftypefn {Built-in Function} {@var{str} =} fgets (@var{fid})\n\
384 @deftypefnx {Built-in Function} {@var{str} =} fgets (@var{fid}, @var{len})\n\
385 Read characters from a file, stopping after a newline, or EOF,\n\
386 or @var{len} characters have been read. The characters read, including\n\
387 the possible trailing newline, are returned as a string.\n\
389 If @var{len} is omitted, @code{fgets} reads until the next newline\n\
392 If there are no more characters to read, @code{fgets} returns @minus{}1.\n\
394 To read a line and discard the terminating newline see @code{fgetl}.\n\
395 @seealso{fputs, fgetl, fscanf, fread, fopen}\n\
398 static std::string who =
"fgets";
405 int nargin = args.
length ();
407 if (nargin == 1 || nargin == 2)
417 std::string tmp = os.
gets (len_arg, err, who);
421 retval(1) = tmp.
length ();
432 DEFUN (fskipl, args, ,
434 @deftypefn {Built-in Function} {@var{nlines} =} fskipl (@var{fid})\n\
435 @deftypefnx {Built-in Function} {@var{nlines} =} fskipl (@var{fid}, @var{count})\n\
436 @deftypefnx {Built-in Function} {@var{nlines} =} fskipl (@var{fid}, Inf)\n\
437 Read and skip @var{count} lines from the file descriptor @var{fid}.\n\
438 @code{fskipl} discards characters until an end-of-line is encountered exactly\n\
439 @var{count}-times, or until the end-of-file marker is found.\n\
441 If @var{count} is omitted, it defaults to 1. @var{count} may also be\n\
442 @code{Inf}, in which case lines are skipped until the end of the file.\n\
443 This form is suitable for counting the number of lines in a file.\n\
445 Returns the number of lines skipped (end-of-line sequences encountered).\n\
446 @seealso{fgetl, fgets, fscanf, fopen}\n\
449 static std::string who =
"fskipl";
453 int nargin = args.
length ();
455 if (nargin == 1 || nargin == 2)
465 off_t tmp = os.
skipl (count_arg, err, who);
480 const std::string& arch,
int& fid)
486 std::string mode = mode_arg;
487 bool use_zlib =
false;
503 if (! (md & std::ios::out
515 "fopen: file found in load path");
523 #if defined (HAVE_ZLIB)
526 FILE *
fptr = gnulib::fopen (fname.c_str (), mode.c_str ());
528 int fd = fileno (fptr);
530 gzFile gzf = ::gzdopen (fd, mode.c_str ());
536 retval.
error (gnulib::strerror (errno));
541 FILE *
fptr = gnulib::fopen (fname.c_str (), mode.c_str ());
547 retval.error (gnulib::strerror (errno));
578 ::error (
"%s: architecture type must be a string", fcn);
581 ::error (
"%s: file mode must be a string", fcn);
584 ::error (
"%s: file name must be a string", fcn);
589 DEFUN (fopen, args, nargout,
591 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} fopen (@var{name}, @var{mode}, @var{arch})\n\
592 @deftypefnx {Built-in Function} {@var{fid_list} =} fopen (\"all\")\n\
593 @deftypefnx {Built-in Function} {[@var{file}, @var{mode}, @var{arch}] =} fopen (@var{fid})\n\
594 The first form of the @code{fopen} function opens the named file with\n\
595 the specified mode (read-write, read-only, etc.) and architecture\n\
596 interpretation (IEEE big endian, IEEE little endian, etc.), and returns\n\
597 an integer value that may be used to refer to the file later. If an\n\
598 error occurs, @var{fid} is set to @minus{}1 and @var{msg} contains the\n\
599 corresponding system error message. The @var{mode} is a one or two\n\
600 character string that specifies whether the file is to be opened for\n\
601 reading, writing, or both.\n\
603 The second form of the @code{fopen} function returns a vector of file ids\n\
604 corresponding to all the currently open files, excluding the\n\
605 @code{stdin}, @code{stdout}, and @code{stderr} streams.\n\
607 The third form of the @code{fopen} function returns information about the\n\
608 open file given its file id.\n\
613 myfile = fopen (\"splat.dat\", \"r\", \"ieee-le\");\n\
617 opens the file @file{splat.dat} for reading. If necessary, binary\n\
618 numeric values will be read assuming they are stored in IEEE format with\n\
619 the least significant bit first, and then converted to the native\n\
622 Opening a file that is already open simply opens it again and returns a\n\
623 separate file id. It is not an error to open a file several times,\n\
624 though writing to the same file through several different file ids may\n\
625 produce unexpected results.\n\
627 The possible values @samp{mode} may have are\n\
631 Open a file for reading.\n\
634 Open a file for writing. The previous contents are discarded.\n\
637 Open or create a file for writing at the end of the file.\n\
640 Open an existing file for reading and writing.\n\
643 Open a file for reading or writing. The previous contents are\n\
647 Open or create a file for reading or writing at the end of the\n\
651 Append a @qcode{\"t\"} to the mode string to open the file in text mode or a\n\
652 @qcode{\"b\"} to open in binary mode. On Windows and Macintosh systems, text\n\
653 mode reading and writing automatically converts linefeeds to the\n\
654 appropriate line end character for the system (carriage-return linefeed\n\
655 on Windows, carriage-return on Macintosh). The default if no mode is\n\
656 specified is binary mode.\n\
658 Additionally, you may append a @qcode{\"z\"} to the mode string to open a\n\
659 gzipped file for reading or writing. For this to be successful, you\n\
660 must also open the file in binary mode.\n\
662 The parameter @var{arch} is a string specifying the default data format\n\
663 for the file. Valid values for @var{arch} are:\n\
667 The format of the current machine (this is the default).\n\
670 IEEE big endian format.\n\
673 IEEE little endian format.\n\
677 however, conversions are currently only supported for @samp{native}\n\
678 @samp{ieee-be}, and @samp{ieee-le} formats.\n\
679 @seealso{fclose, fgets, fgetl, fscanf, fread, fputs, fdisp, fprintf, fwrite, fskipl, fseek, frewind, ftell, feof, ferror, fclear, fflush, freport}\n\
686 int nargin = args.
length ();
690 if (args(0).is_string ())
697 if (nargout < 2 && args(0).string_value () ==
"all")
715 if (nargin > 0 && nargin < 4)
734 int error_number = 0;
736 retval(1) = os.
error (
false, error_number);
746 DEFUN (freport, args, ,
748 @deftypefn {Built-in Function} {} freport ()\n\
749 Print a list of which files have been opened, and whether they are open\n\
750 for reading, writing, or both. For example:\n\
756 @print{} number mode name\n\
758 @print{} 0 r stdin\n\
759 @print{} 1 w stdout\n\
760 @print{} 2 w stderr\n\
761 @print{} 3 r myfile\n\
764 @seealso{fopen, fclose}\n\
769 int nargin = args.
length ();
772 warning (
"freport: ignoring extra arguments");
779 DEFUN (frewind, args, nargout,
781 @deftypefn {Built-in Function} {} frewind (@var{fid})\n\
782 Move the file pointer to the beginning of the file @var{fid}, returning\n\
783 0 for success, and -1 if an error was encountered. It is equivalent to\n\
784 @code{fseek (@var{fid}, 0, SEEK_SET)}.\n\
785 @seealso{fseek, ftell, fopen}\n\
792 int nargin = args.
length ();
810 DEFUN (fseek, args, ,
812 @deftypefn {Built-in Function} {} fseek (@var{fid}, @var{offset})\n\
813 @deftypefnx {Built-in Function} {} fseek (@var{fid}, @var{offset}, @var{origin})\n\
814 @deftypefnx {Built-in Function} {@var{status} =} fseek (@dots{})\n\
815 Set the file pointer to any location within the file @var{fid}.\n\
817 The pointer is positioned @var{offset} characters from the @var{origin},\n\
818 which may be one of the predefined variables @w{@code{SEEK_CUR}} (current\n\
819 position), @w{@code{SEEK_SET}} (beginning), or @w{@code{SEEK_END}} (end of\n\
820 file) or strings @qcode{\"cof\"}, @qcode{\"bof\"} or @qcode{\"eof\"}. If\n\
821 @var{origin} is omitted, @w{@code{SEEK_SET}} is assumed. @var{offset} may\n\
822 be positive, negative, or zero but not all combinations of @var{origin} and\n\
823 @var{offset} can be realized.\n\
825 Return 0 on success and -1 on error.\n\
826 @seealso{fskipl, frewind, ftell, fopen}\n\
831 int nargin = args.
length ();
833 if (nargin == 2 || nargin == 3)
842 retval = os.
seek (args(1), origin_arg);
851 DEFUN (ftell, args, ,
853 @deftypefn {Built-in Function} {} ftell (@var{fid})\n\
854 Return the position of the file pointer as the number of characters\n\
855 from the beginning of the file @var{fid}.\n\
856 @seealso{fseek, feof, fopen}\n\
861 int nargin = args.
length ();
876 DEFUN (fprintf, args, nargout,
878 @deftypefn {Built-in Function} {} fprintf (@var{fid}, @var{template}, @dots{})\n\
879 This function is just like @code{printf}, except that the output is\n\
880 written to the stream @var{fid} instead of @code{stdout}.\n\
881 If @var{fid} is omitted, the output is written to @code{stdout}.\n\
882 @seealso{fputs, fdisp, fwrite, fscanf, printf, sprintf, fopen}\n\
885 static std::string who =
"fprintf";
891 int nargin = args.
length ();
893 if (nargin > 1 || (nargin > 0 && args(0).is_string ()))
898 if (args(0).is_string ())
910 if (args(fmt_n).is_string ())
914 if (nargin > 1 + fmt_n)
918 for (
int i = fmt_n + 1; i < nargin; i++)
919 tmp_args(i-fmt_n-1) = args(i);
922 result = os.
printf (args(fmt_n), tmp_args, who);
925 ::error (
"%s: format TEMPLATE must be a string", who.c_str ());
937 DEFUN (printf, args, nargout,
939 @deftypefn {Built-in Function} {} printf (@var{template}, @dots{})\n\
940 Print optional arguments under the control of the template string\n\
941 @var{template} to the stream @code{stdout} and return the number of\n\
942 characters printed.\n\
943 @ifclear OCTAVE_MANUAL\n\
945 See the Formatted Output section of the GNU Octave manual for a\n\
946 complete description of the syntax of the template string.\n\
948 @seealso{fprintf, sprintf, scanf}\n\
951 static std::string who =
"printf";
957 int nargin = args.
length ();
961 if (args(0).is_string ())
969 for (
int i = 1; i < nargin; i++)
970 tmp_args(i-1) = args(i);
973 result = stdout_stream.
printf (args(0), tmp_args, who);
976 ::error (
"%s: format TEMPLATE must be a string", who.c_str ());
987 DEFUN (fputs, args, ,
989 @deftypefn {Built-in Function} {} fputs (@var{fid}, @var{string})\n\
990 Write a string to a file with no formatting.\n\
992 Return a non-negative number on success and EOF on error.\n\
993 @seealso{fdisp, fprintf, fwrite, fopen}\n\
996 static std::string who =
"fputs";
1000 int nargin = args.
length ();
1007 retval = os.
puts (args(1), who);
1015 DEFUN (puts, args, ,
1017 @deftypefn {Built-in Function} {} puts (@var{string})\n\
1018 Write a string to the standard output with no formatting.\n\
1020 Return a non-negative number on success and EOF on error.\n\
1021 @seealso{fputs, disp}\n\
1024 static std::string who =
"puts";
1028 if (args.length () == 1)
1029 retval = stdout_stream.
puts (args(0), who);
1036 DEFUN (sprintf, args, ,
1038 @deftypefn {Built-in Function} {} sprintf (@var{template}, @dots{})\n\
1039 This is like @code{printf}, except that the output is returned as a\n\
1040 string. Unlike the C library function, which requires you to provide a\n\
1041 suitably sized string as an argument, Octave's @code{sprintf} function\n\
1042 returns the string, automatically sized to hold all of the items\n\
1044 @seealso{printf, fprintf, sscanf}\n\
1047 static std::string who =
"sprintf";
1051 int nargin = args.
length ();
1056 retval(1) =
"unknown error";
1075 for (
int i = 1; i < nargin; i++)
1076 tmp_args(i-1) = args(i);
1079 retval(2) = os.
printf (fmt_arg, tmp_args, who);
1080 retval(1) = os.
error ();
1085 ::error (
"%s: format TEMPLATE must be a string", who.c_str ());
1088 ::error (
"%s: unable to create output buffer", who.c_str ());
1096 DEFUN (fscanf, args, ,
1098 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{errmsg}] =} fscanf (@var{fid}, @var{template}, @var{size})\n\
1099 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}] =} fscanf (@var{fid}, @var{template}, \"C\")\n\
1100 In the first form, read from @var{fid} according to @var{template},\n\
1101 returning the result in the matrix @var{val}.\n\
1103 The optional argument @var{size} specifies the amount of data to read\n\
1104 and may be one of\n\
1108 Read as much as possible, returning a column vector.\n\
1111 Read up to @var{nr} elements, returning a column vector.\n\
1113 @item [@var{nr}, Inf]\n\
1114 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\
1115 number of elements read is not an exact multiple of @var{nr}, the last\n\
1116 column is padded with zeros.\n\
1118 @item [@var{nr}, @var{nc}]\n\
1119 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\
1120 @var{nr} rows. If the number of elements read is not an exact multiple\n\
1121 of @var{nr}, the last column is padded with zeros.\n\
1125 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\
1127 A string is returned if @var{template} specifies only character\n\
1130 The number of items successfully read is returned in @var{count}.\n\
1132 If an error occurs, @var{errmsg} contains a system-dependent error message.\n\
1134 In the second form, read from @var{fid} according to @var{template},\n\
1135 with each conversion specifier in @var{template} corresponding to a\n\
1136 single scalar return value. This form is more ``C-like'', and also\n\
1137 compatible with previous versions of Octave. The number of successful\n\
1138 conversions is returned in @var{count}\n\
1139 @ifclear OCTAVE_MANUAL\n\
1141 See the Formatted Input section of the GNU Octave manual for a\n\
1142 complete description of the syntax of the template string.\n\
1144 @seealso{fgets, fgetl, fread, scanf, sscanf, fopen}\n\
1147 static std::string who =
"fscanf";
1151 int nargin = args.
length ();
1153 if (nargin == 3 && args(2).is_string ())
1159 if (args(1).is_string ())
1160 retval = os.
oscanf (args(1), who);
1162 ::error (
"%s: format TEMPLATE must be a string", who.c_str ());
1167 retval(2) =
"unknown error";
1171 if (nargin == 2 || nargin == 3)
1177 if (args(1).is_string ())
1182 ? args(2).vector_value ()
1192 retval(2) = os.
error ();
1199 ::error (
"%s: format must be a string", who.c_str ());
1221 ::error (
"sscanf: argument STRING must be a string");
1226 DEFUN (sscanf, args, ,
1228 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{errmsg}, @var{pos}] =} sscanf (@var{string}, @var{template}, @var{size})\n\
1229 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}] =} sscanf (@var{string}, @var{template}, \"C\")\n\
1230 This is like @code{fscanf}, except that the characters are taken from the\n\
1231 string @var{string} instead of from a stream. Reaching the end of the\n\
1232 string is treated as an end-of-file condition. In addition to the values\n\
1233 returned by @code{fscanf}, the index of the next character to be read\n\
1234 is returned in @var{pos}.\n\
1235 @seealso{fscanf, scanf, sprintf}\n\
1238 static std::string who =
"sscanf";
1242 int nargin = args.
length ();
1244 if (nargin == 3 && args(2).is_string ())
1254 if (args(1).is_string ())
1255 retval = os.
oscanf (args(1), who);
1257 ::error (
"%s: format TEMPLATE must be a string", who.c_str ());
1260 ::error (
"%s: unable to create temporary input buffer",
1264 ::error (
"%s: argument STRING must be a string", who.c_str ());
1268 if (nargin == 2 || nargin == 3)
1271 retval(2) =
"unknown error";
1283 if (args(1).is_string ())
1288 ? args(2).vector_value ()
1299 std::string errmsg = os.
error ();
1302 = (os.
eof () ? data.length () : os.
tell ()) + 1;
1309 ::error (
"%s: format TEMPLATE must be a string",
1313 ::error (
"%s: unable to create temporary input buffer",
1324 DEFUN (scanf, args, nargout,
1326 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{errmsg}] =} scanf (@var{template}, @var{size})\n\
1327 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}]] =} scanf (@var{template}, \"C\")\n\
1328 This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.\n\
1330 It is currently not useful to call @code{scanf} in interactive\n\
1332 @seealso{fscanf, sscanf, printf}\n\
1335 int nargin = args.length ();
1340 for (
int i = 0; i < nargin; i++)
1341 tmp_args (i+1) = args (i);
1343 return Ffscanf (tmp_args, nargout);
1368 input_type, output_type);
1384 retval = os.
read (size, block_size, input_type,
1385 output_type, skip, flt_fmt, count);
1388 ::error (
"fread: ARCH architecture type must be a string");
1391 ::error (
"fread: SKIP must be an integer");
1394 ::error (
"fread: invalid PRECISION specified");
1397 ::error (
"fread: PRECISION must be a string");
1400 ::error (
"fread: invalid SIZE specified");
1405 DEFUN (fread, args, ,
1407 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip}, @var{arch})\n\
1408 Read binary data of type @var{precision} from the specified file ID\n\
1411 The optional argument @var{size} specifies the amount of data to read\n\
1412 and may be one of\n\
1416 Read as much as possible, returning a column vector.\n\
1419 Read up to @var{nr} elements, returning a column vector.\n\
1421 @item [@var{nr}, Inf]\n\
1422 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\
1423 number of elements read is not an exact multiple of @var{nr}, the last\n\
1424 column is padded with zeros.\n\
1426 @item [@var{nr}, @var{nc}]\n\
1427 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\
1428 @var{nr} rows. If the number of elements read is not an exact multiple\n\
1429 of @var{nr}, the last column is padded with zeros.\n\
1433 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\
1435 The optional argument @var{precision} is a string specifying the type of\n\
1436 data to read and may be one of\n\
1439 @item @qcode{\"schar\"}\n\
1440 @itemx @qcode{\"signed char\"}\n\
1441 Signed character.\n\
1443 @item @qcode{\"uchar\"}\n\
1444 @itemx @qcode{\"unsigned char\"}\n\
1445 Unsigned character.\n\
1447 @item @qcode{\"int8\"}\n\
1448 @itemx @qcode{\"integer*1\"}\n\
1450 8-bit signed integer.\n\
1452 @item @qcode{\"int16\"}\n\
1453 @itemx @qcode{\"integer*2\"}\n\
1454 16-bit signed integer.\n\
1456 @item @qcode{\"int32\"}\n\
1457 @itemx @qcode{\"integer*4\"}\n\
1458 32-bit signed integer.\n\
1460 @item @qcode{\"int64\"}\n\
1461 @itemx @qcode{\"integer*8\"}\n\
1462 64-bit signed integer.\n\
1464 @item @qcode{\"uint8\"}\n\
1465 8-bit unsigned integer.\n\
1467 @item @qcode{\"uint16\"}\n\
1468 16-bit unsigned integer.\n\
1470 @item @qcode{\"uint32\"}\n\
1471 32-bit unsigned integer.\n\
1473 @item @qcode{\"uint64\"}\n\
1474 64-bit unsigned integer.\n\
1476 @item @qcode{\"single\"}\n\
1477 @itemx @qcode{\"float32\"}\n\
1478 @itemx @qcode{\"real*4\"}\n\
1479 32-bit floating point number.\n\
1481 @item @qcode{\"double\"}\n\
1482 @itemx @qcode{\"float64\"}\n\
1483 @itemx @qcode{\"real*8\"}\n\
1484 64-bit floating point number.\n\
1486 @item @qcode{\"char\"}\n\
1487 @itemx @qcode{\"char*1\"}\n\
1488 Single character.\n\
1490 @item @qcode{\"short\"}\n\
1491 Short integer (size is platform dependent).\n\
1493 @item @qcode{\"int\"}\n\
1494 Integer (size is platform dependent).\n\
1496 @item @qcode{\"long\"}\n\
1497 Long integer (size is platform dependent).\n\
1499 @item @qcode{\"ushort\"}\n\
1500 @itemx @qcode{\"unsigned short\"}\n\
1501 Unsigned short integer (size is platform dependent).\n\
1503 @item @qcode{\"uint\"}\n\
1504 @itemx @qcode{\"unsigned int\"}\n\
1505 Unsigned integer (size is platform dependent).\n\
1507 @item @qcode{\"ulong\"}\n\
1508 @itemx @qcode{\"unsigned long\"}\n\
1509 Unsigned long integer (size is platform dependent).\n\
1511 @item @qcode{\"float\"}\n\
1512 Single precision floating point number (size is platform dependent).\n\
1516 The default precision is @qcode{\"uchar\"}.\n\
1518 The @var{precision} argument may also specify an optional repeat\n\
1519 count. For example, @samp{32*single} causes @code{fread} to read\n\
1520 a block of 32 single precision floating point numbers. Reading in\n\
1521 blocks is useful in combination with the @var{skip} argument.\n\
1523 The @var{precision} argument may also specify a type conversion.\n\
1524 For example, @samp{int16=>int32} causes @code{fread} to read 16-bit\n\
1525 integer values and return an array of 32-bit integer values. By\n\
1526 default, @code{fread} returns a double precision array. The special\n\
1527 form @samp{*TYPE} is shorthand for @samp{TYPE=>TYPE}.\n\
1529 The conversion and repeat counts may be combined. For example, the\n\
1530 specification @samp{32*single=>single} causes @code{fread} to read\n\
1531 blocks of single precision floating point values and return an array\n\
1532 of single precision values instead of the default array of double\n\
1533 precision values.\n\
1535 The optional argument @var{skip} specifies the number of bytes to skip\n\
1536 after each element (or block of elements) is read. If it is not\n\
1537 specified, a value of 0 is assumed. If the final block read is not\n\
1538 complete, the final skip is omitted. For example,\n\
1541 fread (f, 10, \"3*single=>single\", 8)\n\
1545 will omit the final 8-byte skip because the last read will not be\n\
1546 a complete block of 3 values.\n\
1548 The optional argument @var{arch} is a string specifying the data format\n\
1549 for the file. Valid values are\n\
1552 @item @qcode{\"native\"}\n\
1553 The format of the current machine.\n\
1555 @item \"ieee-be\"\n\
1558 @item \"ieee-le\"\n\
1559 IEEE little endian.\n\
1562 The data read from the file is returned in @var{val}, and the number of\n\
1563 values read is returned in @code{count}\n\
1564 @seealso{fwrite, fgets, fgetl, fscanf, fopen}\n\
1569 int nargin = args.
length ();
1571 if (nargin > 0 && nargin < 6)
1587 if (nargin > idx && ! args(idx).is_string ())
1648 retval = os.
write (data, block_size, output_type,
1652 ::error (
"fwrite: ARCH architecture type must be a string");
1655 ::error (
"fwrite: SKIP must be an integer");
1658 ::error (
"fwrite: invalid PRECISION specified");
1661 ::error (
"fwrite: PRECISION must be a string");
1666 DEFUN (fwrite, args, ,
1668 @deftypefn {Built-in Function} {@var{count} =} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip}, @var{arch})\n\
1669 Write data in binary form of type @var{precision} to the specified file\n\
1670 ID @var{fid}, returning the number of values successfully written to the\n\
1673 The argument @var{data} is a matrix of values that are to be written to\n\
1674 the file. The values are extracted in column-major order.\n\
1676 The remaining arguments @var{precision}, @var{skip}, and @var{arch} are\n\
1677 optional, and are interpreted as described for @code{fread}.\n\
1679 The behavior of @code{fwrite} is undefined if the values in @var{data}\n\
1680 are too large to fit in the specified precision.\n\
1681 @seealso{fread, fputs, fprintf, fopen}\n\
1686 int nargin = args.
length ();
1688 if (nargin > 1 && nargin < 6)
1716 double status =
do_fwrite (os, data, prec, skip, arch);
1729 @deftypefn {Built-in Function} {} feof (@var{fid})\n\
1730 Return 1 if an end-of-file condition has been encountered for a given\n\
1731 file and 0 otherwise. Note that it will only return 1 if the end of the\n\
1732 file has already been encountered, not if the next read operation will\n\
1733 result in an end-of-file condition.\n\
1734 @seealso{fread, fopen}\n\
1739 int nargin = args.
length ();
1746 retval = os.
eof () ? 1.0 : 0.0;
1756 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} ferror (@var{fid})\n\
1757 @deftypefnx {Built-in Function} {[@var{err}, @var{msg}] =} ferror (@var{fid}, \"clear\")\n\
1758 Return 1 if an error condition has been encountered for the file ID\n\
1759 @var{fid} and 0 otherwise. Note that it will only return 1 if an error\n\
1760 has already been encountered, not if the next operation will result in\n\
1761 an error condition.\n\
1763 The second argument is optional. If it is supplied, also clear the\n\
1765 @seealso{fclear, fopen}\n\
1770 int nargin = args.
length ();
1772 if (nargin == 1 || nargin == 2)
1782 std::string opt = args(1).string_value ();
1785 clear = (opt ==
"clear");
1790 int error_number = 0;
1792 std::string error_message = os.
error (clear, error_number);
1794 retval(1) = error_number;
1795 retval(0) = error_message;
1804 DEFUN (popen, args, ,
1806 @deftypefn {Built-in Function} {@var{fid} =} popen (@var{command}, @var{mode})\n\
1807 Start a process and create a pipe. The name of the command to run is\n\
1808 given by @var{command}. The file identifier corresponding to the input\n\
1809 or output stream of the process is returned in @var{fid}. The argument\n\
1810 @var{mode} may be\n\
1813 @item @qcode{\"r\"}\n\
1814 The pipe will be connected to the standard output of the process, and\n\
1815 open for reading.\n\
1817 @item @qcode{\"w\"}\n\
1818 The pipe will be connected to the standard input of the process, and\n\
1819 open for writing.\n\
1826 fid = popen (\"ls -ltr / | tail -3\", \"r\");\n\
1827 while (ischar (s = fgets (fid)))\n\
1828 fputs (stdout, s);\n\
1831 @print{} drwxr-xr-x 33 root root 3072 Feb 15 13:28 etc\n\
1832 @print{} drwxr-xr-x 3 root root 1024 Feb 15 13:28 lib\n\
1833 @print{} drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp\n\
1840 int nargin = args.
length ();
1844 std::string name = args(0).string_value ();
1848 std::string mode = args(1).string_value ();
1858 else if (mode ==
"w")
1865 ::error (
"popen: invalid MODE specified");
1868 ::error (
"popen: MODE must be a string");
1871 ::error (
"popen: COMMAND must be a string");
1881 @deftypefn {Built-in Function} {} pclose (@var{fid})\n\
1882 Close a file identifier that was opened by @code{popen}. You may also\n\
1883 use @code{fclose} for the same purpose.\n\
1888 int nargin = args.
length ();
1900 @c List other forms of function in documentation index\n\
1901 @findex octave_tmp_file_name\n\
1903 @deftypefn {Built-in Function} {} tmpnam ()\n\
1904 @deftypefnx {Built-in Function} {} tmpnam (@var{dir})\n\
1905 @deftypefnx {Built-in Function} {} tmpnam (@var{dir}, @var{prefix})\n\
1906 Return a unique temporary file name as a string.\n\
1908 If @var{prefix} is omitted, a value of @qcode{\"oct-\"} is used.\n\
1909 If @var{dir} is also omitted, the default directory for temporary files\n\
1910 is used. If @var{dir} is provided, it must exist, otherwise the default\n\
1911 directory for temporary files is used. Since the named file is not\n\
1912 opened, by @code{tmpnam}, it is possible (though relatively unlikely)\n\
1913 that it will not be available by the time your program attempts to open it.\n\
1914 @seealso{tmpfile, mkstemp, P_tmpdir}\n\
1919 int len = args.
length ();
1923 std::string dir = len > 0 ? args(0).string_value () : std::string ();
1928 = len > 1 ? args(1).string_value () : std::string (
"oct-");
1933 ::error (
"PREFIX must be a string");
1936 ::error (
"DIR argument must be a string");
1944 DEFALIAS (octave_tmp_file_name, tmpnam);
1946 DEFUN (tmpfile, args, ,
1948 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} tmpfile ()\n\
1949 Return the file ID corresponding to a new temporary file with a unique\n\
1950 name. The file is opened in binary read/write (@qcode{\"w+b\"}) mode.\n\
1951 The file will be deleted automatically when it is closed or when Octave\n\
1954 If successful, @var{fid} is a valid file ID and @var{msg} is an empty\n\
1955 string. Otherwise, @var{fid} is -1 and @var{msg} contains a\n\
1956 system-dependent error message.\n\
1957 @seealso{tmpnam, mkstemp, P_tmpdir}\n\
1962 retval(1) = std::string ();
1965 int nargin = args.
length ();
1969 FILE *fid = gnulib::tmpfile ();
1982 error (
"tmpfile: failed to create octave_stdiostream object");
1987 retval(1) = gnulib::strerror (errno);
1997 DEFUN (mkstemp, args, ,
1999 @deftypefn {Built-in Function} {[@var{fid}, @var{name}, @var{msg}] =} mkstemp (@var{template}, @var{delete})\n\
2000 Return the file ID corresponding to a new temporary file with a unique\n\
2001 name created from @var{template}. The last six characters of @var{template}\n\
2002 must be @code{XXXXXX} and these are replaced with a string that makes the\n\
2003 filename unique. The file is then created with mode read/write and\n\
2004 permissions that are system dependent (on GNU/Linux systems, the permissions\n\
2005 will be 0600 for versions of glibc 2.0.7 and later). The file is opened\n\
2006 in binary mode and with the @w{@code{O_EXCL}} flag.\n\
2008 If the optional argument @var{delete} is supplied and is true,\n\
2009 the file will be deleted automatically when Octave exits.\n\
2011 If successful, @var{fid} is a valid file ID, @var{name} is the name of\n\
2012 the file, and @var{msg} is an empty string. Otherwise, @var{fid}\n\
2013 is -1, @var{name} is empty, and @var{msg} contains a system-dependent\n\
2015 @seealso{tmpfile, tmpnam, P_tmpdir}\n\
2020 retval(2) = std::string ();
2021 retval(1) = std::string ();
2024 int nargin = args.
length ();
2026 if (nargin == 1 || nargin == 2)
2028 std::string tmpl8 = args(0).string_value ();
2033 strcpy (tmp, tmpl8.c_str ());
2035 int fd = gnulib::mkostemp (tmp, O_BINARY);
2039 retval(2) = gnulib::strerror (errno);
2044 const char *fopen_mode =
"w+b";
2046 FILE *fid = fdopen (fd, fopen_mode);
2050 std::string nm = tmp;
2061 if (nargin == 2 && args(1).
is_true ())
2065 error (
"mkstemp: failed to create octave_stdiostream object");
2069 retval(2) = gnulib::strerror (errno);
2075 error (
"mkstemp: TEMPLATE argument must be a string");
2088 int tmp = x % obase;
2090 if (tmp > ibase - 1)
2091 ::error (
"umask: invalid digit");
2096 while ((x = (x - tmp) / obase))
2099 if (tmp > ibase - 1)
2101 ::error (
"umask: invalid digit");
2104 retval += mult * tmp;
2114 @deftypefn {Built-in Function} {} umask (@var{mask})\n\
2115 Set the permission mask for file creation. The parameter @var{mask}\n\
2116 is an integer, interpreted as an octal number. If successful,\n\
2117 returns the previous value of the mask (as an integer to be\n\
2118 interpreted as an octal number); otherwise an error message is printed.\n\
2125 if (args.length () == 1)
2127 int mask = args(0).int_value (
true);
2134 ::error (
"umask: MASK must be a positive integer value");
2138 int oct_mask =
convert (mask, 8, 10);
2147 ::error (
"umask: MASK must be an integer");
2164 int nargin = args.
length ();
2176 @deftypefn {Built-in Function} {} P_tmpdir ()\n\
2177 Return the default name of the directory for temporary files on\n\
2178 this system. The name of this directory is system dependent.\n\
2183 int nargin = args.
length ();
2198 @deftypefn {Built-in Function} {} SEEK_SET ()\n\
2199 @deftypefnx {Built-in Function} {} SEEK_CUR ()\n\
2200 @deftypefnx {Built-in Function} {} SEEK_END ()\n\
2201 Return the numerical value to pass to @code{fseek} to perform\n\
2202 one of the following actions:\n\
2206 Position file relative to the beginning.\n\
2209 Position file relative to the current position.\n\
2212 Position file relative to the end.\n\
2222 @deftypefn {Built-in Function} {} SEEK_CUR ()\n\
2223 Return the numerical value to pass to @code{fseek} to\n\
2224 position the file pointer relative to the current position.\n\
2225 @seealso{SEEK_SET, SEEK_END}\n\
2233 @deftypefn {Built-in Function} {} SEEK_END ()\n\
2234 Return the numerical value to pass to @code{fseek} to\n\
2235 position the file pointer relative to the end of the file.\n\
2236 @seealso{SEEK_SET, SEEK_CUR}\n\
2248 int nargin = args.
length ();
2260 @deftypefn {Built-in Function} {} stdin ()\n\
2261 Return the numeric value corresponding to the standard input stream.\n\
2262 When Octave is used interactively, this is filtered through the command\n\
2263 line editing functions.\n\
2264 @seealso{stdout, stderr}\n\
2272 @deftypefn {Built-in Function} {} stdout ()\n\
2273 Return the numeric value corresponding to the standard output stream.\n\
2274 Data written to the standard output is normally filtered through the pager.\n\
2275 @seealso{stdin, stderr}\n\
2283 @deftypefn {Built-in Function} {} stderr ()\n\
2284 Return the numeric value corresponding to the standard error stream.\n\
2285 Even if paging is turned on, the standard error is not sent to the\n\
2286 pager. It is useful for error messages and prompts.\n\
2287 @seealso{stdin, stdout}\n\