Mercurial > octave
comparison src/file-io.cc @ 4233:ccfdb55c8156
[project @ 2002-12-20 22:43:54 by jwe]
author | jwe |
---|---|
date | Fri, 20 Dec 2002 22:43:55 +0000 |
parents | cbac6756967e |
children | df5f2e433a11 |
comparison
equal
deleted
inserted
replaced
4232:b032ebd54586 | 4233:ccfdb55c8156 |
---|---|
158 Closes the specified file. If an error is encountered while trying to\n\ | 158 Closes the specified file. If an error is encountered while trying to\n\ |
159 close the file, an error message is printed and @code{fclose} returns\n\ | 159 close the file, an error message is printed and @code{fclose} returns\n\ |
160 0. Otherwise, it returns 1.\n\ | 160 0. Otherwise, it returns 1.\n\ |
161 @end deftypefn") | 161 @end deftypefn") |
162 { | 162 { |
163 double retval = -1.0; | 163 octave_value retval = -1; |
164 | 164 |
165 int nargin = args.length (); | 165 int nargin = args.length (); |
166 | 166 |
167 if (nargin == 1) | 167 if (nargin == 1) |
168 retval = static_cast<double> (octave_stream_list::remove (args(0), | 168 retval = octave_stream_list::remove (args(0), "fclose"); |
169 "fclose")); | |
170 else | 169 else |
171 print_usage ("fclose"); | 170 print_usage ("fclose"); |
172 | 171 |
173 return retval; | 172 return retval; |
174 } | 173 } |
180 pending output makes it to the screen before some other event occurs.\n\ | 179 pending output makes it to the screen before some other event occurs.\n\ |
181 For example, it is always a good idea to flush the standard output\n\ | 180 For example, it is always a good idea to flush the standard output\n\ |
182 stream before calling @code{input}.\n\ | 181 stream before calling @code{input}.\n\ |
183 @end deftypefn") | 182 @end deftypefn") |
184 { | 183 { |
185 double retval = -1.0; | 184 octave_value retval = -1; |
186 | 185 |
187 int nargin = args.length (); | 186 int nargin = args.length (); |
188 | 187 |
189 if (nargin == 1) | 188 if (nargin == 1) |
190 { | 189 { |
194 | 193 |
195 if (fid == 1) | 194 if (fid == 1) |
196 { | 195 { |
197 flush_octave_stdout (); | 196 flush_octave_stdout (); |
198 | 197 |
199 retval = 0.0; | 198 retval = 0; |
200 } | 199 } |
201 else | 200 else |
202 { | 201 { |
203 octave_stream os = octave_stream_list::lookup (fid, "fflush"); | 202 octave_stream os = octave_stream_list::lookup (fid, "fflush"); |
204 | 203 |
205 if (! error_state) | 204 if (! error_state) |
206 retval = static_cast<double> (os.flush ()); | 205 retval = os.flush (); |
207 } | 206 } |
208 } | 207 } |
209 else | 208 else |
210 print_usage ("fflush"); | 209 print_usage ("fflush"); |
211 | 210 |
225 If there are no more characters to read, @code{fgetl} returns @minus{}1.\n\ | 224 If there are no more characters to read, @code{fgetl} returns @minus{}1.\n\ |
226 @end deftypefn") | 225 @end deftypefn") |
227 { | 226 { |
228 octave_value_list retval; | 227 octave_value_list retval; |
229 | 228 |
230 retval(1) = 0.0; | 229 retval(1) = 0; |
231 retval(0) = -1.0; | 230 retval(0) = -1; |
232 | 231 |
233 int nargin = args.length (); | 232 int nargin = args.length (); |
234 | 233 |
235 if (nargin == 1 || nargin == 2) | 234 if (nargin == 1 || nargin == 2) |
236 { | 235 { |
237 octave_stream os = octave_stream_list::lookup (args(0), "fgetl"); | 236 octave_stream os = octave_stream_list::lookup (args(0), "fgetl"); |
238 | 237 |
239 if (! error_state) | 238 if (! error_state) |
240 { | 239 { |
241 octave_value len_arg = (nargin == 2) | 240 octave_value len_arg = (nargin == 2) |
242 ? args(1) : octave_value (static_cast<double> (INT_MAX)); | 241 ? args(1) : octave_value (INT_MAX); |
243 | 242 |
244 bool err = false; | 243 bool err = false; |
245 | 244 |
246 std::string tmp = os.getl (len_arg, err); | 245 std::string tmp = os.getl (len_arg, err); |
247 | 246 |
283 octave_stream os = octave_stream_list::lookup (args(0), "fgets"); | 282 octave_stream os = octave_stream_list::lookup (args(0), "fgets"); |
284 | 283 |
285 if (! error_state) | 284 if (! error_state) |
286 { | 285 { |
287 octave_value len_arg = (nargin == 2) | 286 octave_value len_arg = (nargin == 2) |
288 ? args(1) : octave_value (static_cast<double> (INT_MAX)); | 287 ? args(1) : octave_value (INT_MAX); |
289 | 288 |
290 bool err = false; | 289 bool err = false; |
291 | 290 |
292 std::string tmp = os.gets (len_arg, err); | 291 std::string tmp = os.gets (len_arg, err); |
293 | 292 |
555 Move the file pointer to the beginning of the file @var{fid}, returning\n\ | 554 Move the file pointer to the beginning of the file @var{fid}, returning\n\ |
556 1 for success, and 0 if an error was encountered. It is equivalent to\n\ | 555 1 for success, and 0 if an error was encountered. It is equivalent to\n\ |
557 @code{fseek (@var{fid}, 0, SEEK_SET)}.\n\ | 556 @code{fseek (@var{fid}, 0, SEEK_SET)}.\n\ |
558 @end deftypefn") | 557 @end deftypefn") |
559 { | 558 { |
560 double retval = -1.0; | 559 octave_value retval = -1; |
561 | 560 |
562 int nargin = args.length (); | 561 int nargin = args.length (); |
563 | 562 |
564 if (nargin == 1) | 563 if (nargin == 1) |
565 { | 564 { |
566 octave_stream os = octave_stream_list::lookup (args(0), "frewind"); | 565 octave_stream os = octave_stream_list::lookup (args(0), "frewind"); |
567 | 566 |
568 if (! error_state) | 567 if (! error_state) |
569 retval = static_cast<double> (os.rewind ()); | 568 retval = os.rewind (); |
570 } | 569 } |
571 else | 570 else |
572 print_usage ("frewind"); | 571 print_usage ("frewind"); |
573 | 572 |
574 return retval; | 573 return retval; |
584 file). If @var{origin} is omitted, @code{SEEK_SET} is assumed. The\n\ | 583 file). If @var{origin} is omitted, @code{SEEK_SET} is assumed. The\n\ |
585 offset must be zero, or a value returned by @code{ftell} (in which case\n\ | 584 offset must be zero, or a value returned by @code{ftell} (in which case\n\ |
586 @var{origin} must be @code{SEEK_SET}.\n\ | 585 @var{origin} must be @code{SEEK_SET}.\n\ |
587 @end deftypefn") | 586 @end deftypefn") |
588 { | 587 { |
589 double retval = -1.0; | 588 octave_value retval = -1; |
590 | 589 |
591 int nargin = args.length (); | 590 int nargin = args.length (); |
592 | 591 |
593 if (nargin == 2 || nargin == 3) | 592 if (nargin == 2 || nargin == 3) |
594 { | 593 { |
597 if (! error_state) | 596 if (! error_state) |
598 { | 597 { |
599 octave_value origin_arg = (nargin == 3) | 598 octave_value origin_arg = (nargin == 3) |
600 ? args(2) : octave_value (-1.0); | 599 ? args(2) : octave_value (-1.0); |
601 | 600 |
602 retval = static_cast<double> (os.seek (args(1), origin_arg)); | 601 retval = os.seek (args(1), origin_arg); |
603 } | 602 } |
604 } | 603 } |
605 else | 604 else |
606 print_usage ("fseek"); | 605 print_usage ("fseek"); |
607 | 606 |
613 @deftypefn {Built-in Function} {} ftell (@var{fid})\n\ | 612 @deftypefn {Built-in Function} {} ftell (@var{fid})\n\ |
614 Return the position of the file pointer as the number of characters\n\ | 613 Return the position of the file pointer as the number of characters\n\ |
615 from the beginning of the file @var{fid}.\n\ | 614 from the beginning of the file @var{fid}.\n\ |
616 @end deftypefn") | 615 @end deftypefn") |
617 { | 616 { |
618 double retval = -1.0; | 617 octave_value retval = -1; |
619 | 618 |
620 int nargin = args.length (); | 619 int nargin = args.length (); |
621 | 620 |
622 if (nargin == 1) | 621 if (nargin == 1) |
623 { | 622 { |
637 @deftypefn {Built-in Function} {} fprintf (@var{fid}, @var{template}, @dots{})\n\ | 636 @deftypefn {Built-in Function} {} fprintf (@var{fid}, @var{template}, @dots{})\n\ |
638 This function is just like @code{printf}, except that the output is\n\ | 637 This function is just like @code{printf}, except that the output is\n\ |
639 written to the stream @var{fid} instead of @code{stdout}.\n\ | 638 written to the stream @var{fid} instead of @code{stdout}.\n\ |
640 @end deftypefn") | 639 @end deftypefn") |
641 { | 640 { |
642 double retval = -1.0; | 641 octave_value retval = -1; |
643 bool return_char_count = true; | 642 bool return_char_count = true; |
644 | 643 |
645 int nargin = args.length (); | 644 int nargin = args.length (); |
646 | 645 |
647 if (nargin > 1 || (nargin > 0 && args(0).is_string ())) | 646 if (nargin > 1 || (nargin > 0 && args(0).is_string ())) |
700 "-*- texinfo -*-\n\ | 699 "-*- texinfo -*-\n\ |
701 @deftypefn {Built-in Function} {} fputs (@var{fid}, @var{string})\n\ | 700 @deftypefn {Built-in Function} {} fputs (@var{fid}, @var{string})\n\ |
702 Write a string to a file with no formatting.\n\ | 701 Write a string to a file with no formatting.\n\ |
703 @end deftypefn") | 702 @end deftypefn") |
704 { | 703 { |
705 double retval = -1.0; | 704 octave_value retval = -1; |
706 | 705 |
707 int nargin = args.length (); | 706 int nargin = args.length (); |
708 | 707 |
709 if (nargin == 2) | 708 if (nargin == 2) |
710 { | 709 { |
757 | 756 |
758 for (int i = 1; i < nargin; i++) | 757 for (int i = 1; i < nargin; i++) |
759 tmp_args(i-1) = args(i); | 758 tmp_args(i-1) = args(i); |
760 } | 759 } |
761 | 760 |
762 retval(2) = static_cast<double> (os.printf (fmt, tmp_args)); | 761 retval(2) = os.printf (fmt, tmp_args); |
763 retval(1) = os.error (); | 762 retval(1) = os.error (); |
764 retval(0) = ostr->str (); | 763 retval(0) = ostr->str (); |
765 } | 764 } |
766 else | 765 else |
767 ::error ("sprintf: format must be a string"); | 766 ::error ("sprintf: format must be a string"); |
861 | 860 |
862 if (! error_state) | 861 if (! error_state) |
863 { | 862 { |
864 octave_value tmp = os.scanf (fmt, size, count); | 863 octave_value tmp = os.scanf (fmt, size, count); |
865 | 864 |
866 retval(1) = static_cast<double> (count); | 865 retval(1) = count; |
867 retval(0) = tmp; | 866 retval(0) = tmp; |
868 } | 867 } |
869 } | 868 } |
870 else | 869 else |
871 ::error ("fscanf: format must be a string"); | 870 ::error ("fscanf: format must be a string"); |
950 // position will clear it. | 949 // position will clear it. |
951 std::string errmsg = os.error (); | 950 std::string errmsg = os.error (); |
952 | 951 |
953 retval(3) = static_cast<double> (os.tell () + 1); | 952 retval(3) = static_cast<double> (os.tell () + 1); |
954 retval(2) = errmsg; | 953 retval(2) = errmsg; |
955 retval(1) = static_cast<double> (count); | 954 retval(1) = count; |
956 retval(0) = tmp; | 955 retval(0) = tmp; |
957 } | 956 } |
958 else | 957 else |
959 ::error ("sscanf: format must be a string"); | 958 ::error ("sscanf: format must be a string"); |
960 } | 959 } |
1199 | 1198 |
1200 int count = -1; | 1199 int count = -1; |
1201 | 1200 |
1202 octave_value tmp = do_fread (os, size, prec, skip, arch, count); | 1201 octave_value tmp = do_fread (os, size, prec, skip, arch, count); |
1203 | 1202 |
1204 retval(1) = static_cast<double> (count); | 1203 retval(1) = count; |
1205 retval(0) = tmp; | 1204 retval(0) = tmp; |
1206 } | 1205 } |
1207 } | 1206 } |
1208 else | 1207 else |
1209 print_usage ("fread"); | 1208 print_usage ("fread"); |
1271 \n\ | 1270 \n\ |
1272 The behavior of @code{fwrite} is undefined if the values in @var{data}\n\ | 1271 The behavior of @code{fwrite} is undefined if the values in @var{data}\n\ |
1273 are too large to fit in the specified precision.\n\ | 1272 are too large to fit in the specified precision.\n\ |
1274 @end deftypefn") | 1273 @end deftypefn") |
1275 { | 1274 { |
1276 octave_value retval = -1.0; | 1275 octave_value retval = -1; |
1277 | 1276 |
1278 int nargin = args.length (); | 1277 int nargin = args.length (); |
1279 | 1278 |
1280 if (nargin > 1 && nargin < 6) | 1279 if (nargin > 1 && nargin < 6) |
1281 { | 1280 { |
1312 file and 0 otherwise. Note that it will only return 1 if the end of the\n\ | 1311 file and 0 otherwise. Note that it will only return 1 if the end of the\n\ |
1313 file has already been encountered, not if the next read operation will\n\ | 1312 file has already been encountered, not if the next read operation will\n\ |
1314 result in an end-of-file condition.\n\ | 1313 result in an end-of-file condition.\n\ |
1315 @end deftypefn") | 1314 @end deftypefn") |
1316 { | 1315 { |
1317 double retval = -1.0; | 1316 octave_value retval = -1; |
1318 | 1317 |
1319 int nargin = args.length (); | 1318 int nargin = args.length (); |
1320 | 1319 |
1321 if (nargin == 1) | 1320 if (nargin == 1) |
1322 { | 1321 { |
1364 | 1363 |
1365 int error_number = 0; | 1364 int error_number = 0; |
1366 | 1365 |
1367 std::string error_message = os.error (clear, error_number); | 1366 std::string error_message = os.error (clear, error_number); |
1368 | 1367 |
1369 retval(1) = static_cast<double> (error_number); | 1368 retval(1) = error_number; |
1370 retval(0) = error_message; | 1369 retval(0) = error_message; |
1371 } | 1370 } |
1372 } | 1371 } |
1373 else | 1372 else |
1374 print_usage ("ferror"); | 1373 print_usage ("ferror"); |
1407 @print{} drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp\n\ | 1406 @print{} drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp\n\ |
1408 @end group\n\ | 1407 @end group\n\ |
1409 @end example\n\ | 1408 @end example\n\ |
1410 @end deftypefn") | 1409 @end deftypefn") |
1411 { | 1410 { |
1412 octave_value retval = -1.0; | 1411 octave_value retval = -1; |
1413 | 1412 |
1414 int nargin = args.length (); | 1413 int nargin = args.length (); |
1415 | 1414 |
1416 if (nargin == 2) | 1415 if (nargin == 2) |
1417 { | 1416 { |
1455 @deftypefn {Built-in Function} {} pclose (@var{fid})\n\ | 1454 @deftypefn {Built-in Function} {} pclose (@var{fid})\n\ |
1456 Close a file identifier that was opened by @code{popen}. You may also\n\ | 1455 Close a file identifier that was opened by @code{popen}. You may also\n\ |
1457 use @code{fclose} for the same purpose.\n\ | 1456 use @code{fclose} for the same purpose.\n\ |
1458 @end deftypefn") | 1457 @end deftypefn") |
1459 { | 1458 { |
1460 double retval = -1.0; | 1459 octave_value retval = -1; |
1461 | 1460 |
1462 int nargin = args.length (); | 1461 int nargin = args.length (); |
1463 | 1462 |
1464 if (nargin == 1) | 1463 if (nargin == 1) |
1465 retval = static_cast<double> (octave_stream_list::remove (args(0), | 1464 retval = octave_stream_list::remove (args(0), "pclose"); |
1466 "pclose")); | |
1467 else | 1465 else |
1468 print_usage ("pclose"); | 1466 print_usage ("pclose"); |
1469 | 1467 |
1470 return retval; | 1468 return retval; |
1471 } | 1469 } |
1569 } | 1567 } |
1570 else | 1568 else |
1571 print_usage ("umask"); | 1569 print_usage ("umask"); |
1572 | 1570 |
1573 if (status >= 0) | 1571 if (status >= 0) |
1574 retval(0) = static_cast<double> (status); | 1572 retval(0) = status; |
1575 | 1573 |
1576 return retval; | 1574 return retval; |
1577 } | 1575 } |
1578 | 1576 |
1579 void | 1577 void |
1580 symbols_of_file_io (void) | 1578 symbols_of_file_io (void) |
1581 { | 1579 { |
1582 // NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be | 1580 // NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be |
1583 // this way for Matlab compatibility. | 1581 // this way for Matlab compatibility. |
1584 | 1582 |
1585 DEFCONSTX ("SEEK_SET", SBV_SEEK_SET, -1.0, | 1583 DEFCONSTX ("SEEK_SET", SBV_SEEK_SET, -1, |
1586 "-*- texinfo -*-\n\ | 1584 "-*- texinfo -*-\n\ |
1587 @defvr {Built-in Variable} SEEK_SET\n\ | 1585 @defvr {Built-in Variable} SEEK_SET\n\ |
1588 @defvrx {Built-in Variable} SEEK_CUR\n\ | 1586 @defvrx {Built-in Variable} SEEK_CUR\n\ |
1589 @defvrx {Built-in Variable} SEEK_END\n\ | 1587 @defvrx {Built-in Variable} SEEK_END\n\ |
1590 These variables may be used as the optional third argument for the\n\ | 1588 These variables may be used as the optional third argument for the\n\ |
1600 @item SEEK_END\n\ | 1598 @item SEEK_END\n\ |
1601 used with fseek to position file relative to the end.\n\ | 1599 used with fseek to position file relative to the end.\n\ |
1602 @end table\n\ | 1600 @end table\n\ |
1603 @end defvr"); | 1601 @end defvr"); |
1604 | 1602 |
1605 DEFCONSTX ("SEEK_CUR", SBV_SEEK_CUR, 0.0, | 1603 DEFCONSTX ("SEEK_CUR", SBV_SEEK_CUR, 0, |
1606 "-*- texinfo -*-\n\ | 1604 "-*- texinfo -*-\n\ |
1607 @defvr {Built-in Variable} SEEK_CUR\n\ | 1605 @defvr {Built-in Variable} SEEK_CUR\n\ |
1608 See SEEK_SET.\n\ | 1606 See SEEK_SET.\n\ |
1609 @end defvr"); | 1607 @end defvr"); |
1610 | 1608 |
1611 DEFCONSTX ("SEEK_END", SBV_SEEK_END, 1.0, | 1609 DEFCONSTX ("SEEK_END", SBV_SEEK_END, 1, |
1612 "-*- texinfo -*-\n\ | 1610 "-*- texinfo -*-\n\ |
1613 @defvr {Built-in Variable} SEEK_END\n\ | 1611 @defvr {Built-in Variable} SEEK_END\n\ |
1614 See SEEK_SET.\n\ | 1612 See SEEK_SET.\n\ |
1615 @end defvr"); | 1613 @end defvr"); |
1616 | 1614 |