1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
|
@c Copyright (C) 1996, 1997 John W. Eaton
@c This is part of the Octave manual.
@c For copying conditions, see the file gpl.texi.
@node Input and Output, Plotting, Error Handling, Top
@chapter Input and Output
There are two distinct classes of input and output functions. The first
set are modeled after the functions available in @sc{Matlab}. The
second set are modeled after the standard I/O library used by the C
programming language and offer more flexibility and control over the
output.
When running interactively, Octave normally sends any output intended
for your terminal that is more than one screen long to a paging program,
such as @code{less} or @code{more}. This avoids the problem of having a
large volume of output stream by before you can read it. With
@code{less} (and some versions of @code{more}) you can also scan forward
and backward, and search for specific items.
Normally, no output is displayed by the pager until just before Octave
is ready to print the top level prompt, or read from the standard input
(for example, by using the @code{fscanf} or @code{scanf} functions).
This means that there may be some delay before any output appears on
your screen if you have asked Octave to perform a significant amount of
work with a single command statement. The function @code{fflush} may be
used to force output to be sent to the pager (or any other stream)
immediately.
You can select the program to run as the pager by setting the variable
@code{PAGER}, and you can turn paging off by setting the value of the
variable @code{page_screen_output} to 0.
@deffn {Command} more
@deffnx {Command} more on
@deffnx {Command} more off
Turn output pagination on or off. Without an argument, @code{more}
toggles the current state.
@end deffn
@defvr {Built-in Variable} PAGER
The default value is normally @code{"less"}, @code{"more"}, or
@code{"pg"}, depending on what programs are installed on your system.
@xref{Installation}.
When running interactively, Octave sends any output intended for your
terminal that is more than one screen long to the program named by the
value of the variable @code{PAGER}.
@end defvr
@defvr {Built-in Variable} page_screen_output
If the value of @code{page_screen_output} is nonzero, all output
intended for the screen that is longer than one page is sent through a
pager. This allows you to view one screenful at a time. Some pagers
(such as @code{less}---see @ref{Installation}) are also capable of moving
backward on the output. The default value is 1.
@end defvr
@defvr {Built-in Variable} page_output_immediately
If the value of @code{page_output_immediately} is nonzero, Octave sends
output to the pager as soon as it is available. Otherwise, Octave
buffers its output and waits until just before the prompt is printed to
flush it to the pager. The default value is 0.
@deftypefn {Built-in Function} {} fflush (@var{fid})
Flush output to @var{fid}. This is useful for ensuring that all
pending output makes it to the screen before some other event occurs.
For example, it is always a good idea to flush the standard output
stream before calling @code{input}.
@end deftypefn
@c XXX FIXME XXX -- maybe this would be a good place to describe the
@c following message:
@c
@c warning: connection to external pager (pid = 9334) lost --
@c warning: pending computations and output may be lost
@c warning: broken pipe
@end defvr
@menu
* Basic Input and Output::
* C-Style I/O Functions::
@end menu
@node Basic Input and Output, C-Style I/O Functions, Input and Output, Input and Output
@section Basic Input and Output
@menu
* Terminal Output::
* Terminal Input::
* Simple File I/O::
@end menu
@node Terminal Output, Terminal Input, Basic Input and Output, Basic Input and Output
@subsection Terminal Output
Since Octave normally prints the value of an expression as soon as it
has been evaluated, the simplest of all I/O functions is a simple
expression. For example, the following expression will display the
value of pi
@example
pi
@print{} pi = 3.1416
@end example
This works well as long as it is acceptable to have the name of the
variable (or @samp{ans}) printed along with the value. To print the
value of a variable without printing its name, use the function
@code{disp}.
The @code{format} command offers some control over the way Octave prints
values with @code{disp} and through the normal echoing mechanism.
@defvr {Built-in Variable} ans
This variable holds the most recently computed result that was not
explicitly assigned to a variable. For example, after the expression
@example
3^2 + 4^2
@end example
@noindent
is evaluated, the value of @code{ans} is 25.
@end defvr
@deftypefn {Built-in Function} {} disp (@var{x})
Display the value of @var{x}. For example,
@example
disp ("The value of pi is:"), disp (pi)
@print{} the value of pi is:
@print{} 3.1416
@end example
@noindent
Note that the output from @code{disp} always ends with a newline.
@end deftypefn
@deffn {Command} format options
Control the format of the output produced by @code{disp} and Octave's
normal echoing mechanism. Valid options are listed in the following
table.
@table @code
@item short
Octave will try to print numbers with at least 3 significant figures
within a field that is a maximum of 8 characters wide.
If Octave is unable to format a matrix so that columns line up on the
decimal point and all the numbers fit within the maximum field width,
it switches to an @samp{e} format.
@item long
Octave will try to print numbers with at least 15 significant figures
within a field that is a maximum of 24 characters wide.
As will the @samp{short} format, Octave will switch to an @samp{e}
format if it is unable to format a matrix so that columns line up on the
decimal point and all the numbers fit within the maximum field width.
@item long e
@itemx short e
The same as @samp{format long} or @samp{format short} but always display
output with an @samp{e} format. For example, with the @samp{short e}
format, pi is displayed as @code{3.14e+00}.
@item long E
@itemx short E
The same as @samp{format long e} or @samp{format short e} but always
display output with an uppercase @samp{E} format. For example, with
the @samp{long E} format, pi is displayed as
@code{3.14159265358979E+00}.
@item free
@itemx none
Print output in free format, without trying to line up columns of
matrices on the decimal point. This also causes complex numbers to be
formatted like this @samp{(0.604194, 0.607088)} instead of like this
@samp{0.60419 + 0.60709i}.
@item bank
Print in a fixed format with two places to the right of the decimal
point.
@item +
Print a @samp{+} symbol for nonzero matrix elements and a space for zero
matrix elements. This format can be very useful for examining the
structure of a large matrix.
@item hex
Print the hexadecimal representation numbers as they are stored in
memory. For example, on a workstation which stores 8 byte real values
in IEEE format with the least significant byte first, the value of
@code{pi} when printed in @code{hex} format is @code{400921fb54442d18}.
This format only works for numeric values.
@item bit
Print the bit representation of numbers as stored in memory.
For example, the value of @code{pi} is
@example
@group
01000000000010010010000111111011
01010100010001000010110100011000
@end group
@end example
(shown here in two 32 bit sections for typesetting purposes) when
printed in bit format on a workstation which stores 8 byte real values
in IEEE format with the least significant byte first. This format only
works for numeric types.
@end table
By default, Octave will try to print numbers with at least 5 significant
figures within a field that is a maximum of 10 characters wide.
If Octave is unable to format a matrix so that columns line up on the
decimal point and all the numbers fit within the maximum field width,
it switches to an @samp{e} format.
If @code{format} is invoked without any options, the default format
state is restored.
@end deffn
@defvr {Built-in Variable} print_answer_id_name
If the value of @code{print_answer_id_name} is nonzero, variable
names are printed along with the result. Otherwise, only the result
values are printed. The default value is 1.
@end defvr
@node Terminal Input, Simple File I/O, Terminal Output, Basic Input and Output
@subsection Terminal Input
Octave has three functions that make it easy to prompt users for
input. The @code{input} and @code{menu} functions are normally
used for managing an interactive dialog with a user, and the
@code{keyboard} function is normally used for doing simple debugging.
@deftypefn {Built-in Function} {} input (@var{prompt})
@deftypefnx {Built-in Function} {} input (@var{prompt}, "s")
Print a prompt and wait for user input. For example,
@example
input ("Pick a number, any number! ")
@end example
@noindent
prints the prompt
@example
Pick a number, any number!
@end example
@noindent
and waits for the user to enter a value. The string entered by the user
is evaluated as an expression, so it may be a literal constant, a
variable name, or any other valid expression.
Currently, @code{input} only returns one value, regardless of the number
of values produced by the evaluation of the expression.
If you are only interested in getting a literal string value, you can
call @code{input} with the character string @code{"s"} as the second
argument. This tells Octave to return the string entered by the user
directly, without evaluating it first.
Because there may be output waiting to be displayed by the pager, it is
a good idea to always call @code{fflush (stdout)} before calling
@code{input}. This will ensure that all pending output is written to
the screen before your prompt. @xref{Input and Output}.
@end deftypefn
@deftypefn {Function File} {} menu (@var{title}, @var{opt1}, @dots{})
Print a title string followed by a series of options. Each option will
be printed along with a number. The return value is the number of the
option selected by the user. This function is useful for interactive
programs. There is no limit to the number of options that may be passed
in, but it may be confusing to present more than will fit easily on one
screen.
@end deftypefn
@deftypefn {Built-in Function} {} keyboard (@var{prompt})
This function is normally used for simple debugging. When the
@code{keyboard} function is executed, Octave prints a prompt and waits
for user input. The input strings are then evaluated and the results
are printed. This makes it possible to examine the values of variables
within a function, and to assign new values to variables. No value is
returned from the @code{keyboard} function, and it continues to prompt
for input until the user types @samp{quit}, or @samp{exit}.
If @code{keyboard} is invoked without any arguments, a default prompt of
@samp{debug> } is used.
@end deftypefn
For both @code{input} and @code{keyboard}, the normal command line
history and editing functions are available at the prompt.
Octave also has a function that makes it possible to get a single
character from the keyboard without requiring the user to type a
carriage return.
@c XXX FIXME XXX -- perhaps kbhit should also be able to print a prompt
@c string?
@deftypefn {Built-in Function} {} kbhit ()
Read a single keystroke from the keyboard. For example,
@example
x = kbhit ();
@end example
@noindent
will set @var{x} to the next character typed at the keyboard as soon as
it is typed.
@end deftypefn
@node Simple File I/O, , Terminal Input, Basic Input and Output
@subsection Simple File I/O
The @code{save} and @code{load} commands allow data to be written to and
read from disk files in various formats. The default format of files
written by the @code{save} command can be controlled using the built-in
variables @code{default_save_format} and @code{save_precision}.
Note that Octave can not yet save or load structure variables or any
user-defined types.
@deffn {Command} save options file v1 v2 @dots{}
Save the named variables @var{v1}, @var{v2}, @dots{} in the file
@var{file}. The special filename @samp{-} can be used to write the
output to your terminal. If no variable names are listed, Octave saves
all the variables in the current scope. Valid options for the
@code{save} command are listed in the following table. Options that
modify the output format override the format specified by the built-in
variable @code{default_save_format}.
@table @code
@item -ascii
Save the data in Octave's text data format.
@item -binary
Save the data in Octave's binary data format.
@item -float-binary
Save the data in Octave's binary data format but only using single
precision. You should use this format only if you know that all the
values to be saved can be represented in single precision.
@item -mat-binary
Save the data in @sc{Matlab}'s binary data format.
@item -save-builtins
Force Octave to save the values of built-in variables too. By default,
Octave does not save built-in variables.
@end table
The list of variables to save may include wildcard patterns containing
the following special characters:
@table @code
@item ?
Match any single character.
@item *
Match zero or more characters.
@item [ @var{list} ]
Match the list of characters specified by @var{list}. If the first
character is @code{!} or @code{^}, match all characters except those
specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will
match all lower and upper case alphabetic characters.
@end table
Except when using the @sc{Matlab} binary data file format, saving global
variables also saves the global status of the variable, so that if it is
restored at a later time using @samp{load}, it will be restored as a
global variable.
The command
@example
save -binary data a b*
@end example
@noindent
saves the variable @samp{a} and all variables beginning with @samp{b} to
the file @file{data} in Octave's binary format.
@end deffn
There are two variables that modify the behavior of @code{save} and one
that controls whether variables are saved when Octave exits unexpectedly.
@defvr {Built-in Variable} crash_dumps_octave_core
If this variable is set to a nonzero value, Octave tries to save all
current variables the the file "octave-core" if it crashes or receives a
hangup, terminate or similar signal. The default value is 1.
@end defvr
@defvr {Built-in Variable} default_save_format
This variable specifies the default format for the @code{save} command.
It should have one of the following values: @code{"ascii"},
@code{"binary"}, @code{float-binary}, or @code{"mat-binary"}. The
initial default save format is Octave's text format.
@end defvr
@defvr {Built-in Variable} save_precision
This variable specifies the number of digits to keep when saving data in
text format. The default value is 17.
@end defvr
@deffn {Command} load options file v1 v2 @dots{}
Load the named variables from the file @var{file}. As with @code{save},
you may specify a list of variables and @code{load} will only extract
those variables with names that match. For example, to restore the
variables saved in the file @file{data}, use the command
@example
load data
@end example
Octave will refuse to overwrite existing variables unless you use the
option @samp{-force}.
If a variable that is not marked as global is loaded from a file when a
global symbol with the same name already exists, it is loaded in the
global symbol table. Also, if a variable is marked as global in a file
and a local symbol exists, the local symbol is moved to the global
symbol table and given the value from the file. Since it seems that
both of these cases are likely to be the result of some sort of error,
they will generate warnings.
The @code{load} command can read data stored in Octave's text and
binary formats, and @sc{Matlab}'s binary format. It will automatically
detect the type of file and do conversion from different floating point
formats (currently only IEEE big and little endian, though other formats
may added in the future).
Valid options for @code{load} are listed in the following table.
@table @code
@item -force
Force variables currently in memory to be overwritten by variables with
the same name found in the file.
@item -ascii
Force Octave to assume the file is in Octave's text format.
@item -binary
Force Octave to assume the file is in Octave's binary format.
@item -mat-binary
Force Octave to assume the file is in @sc{Matlab}'s binary format.
@end table
@end deffn
@node C-Style I/O Functions, , Basic Input and Output, Input and Output
@section C-Style I/O Functions
Octave's C-style input and output functions provide most of the
functionality of the C programming language's standard I/O library. The
argument lists for some of the input functions are slightly different,
however, because Octave has no way of passing arguments by reference.
In the following, @var{file} refers to a file name and @code{fid} refers
to an integer file number, as returned by @code{fopen}.
There are three files that are always available. Although these files
can be accessed using their corresponding numeric file ids, you should
always use the symbolic names given in the table below, since it will
make your programs easier to understand.
@defvr {Built-in Variable} stdin
The standard input stream (file id 0). When Octave is used
interactively, this is filtered through the command line editing
functions.
@end defvr
@defvr {Built-in Variable} stdout
The standard output stream (file id 1). Data written to the
standard output is normally filtered through the pager.
@end defvr
@defvr {Built-in Variable} stderr
The standard error stream (file id 2). Even if paging is turned on,
the standard error is not sent to the pager. It is useful for error
messages and prompts.
@end defvr
@menu
* Opening and Closing Files::
* Simple Output::
* Line-Oriented Input::
* Formatted Output::
* Output Conversion for Matrices::
* Output Conversion Syntax::
* Table of Output Conversions::
* Integer Conversions::
* Floating-Point Conversions:: Other Output Conversions::
* Other Output Conversions::
* Formatted Input::
* Input Conversion Syntax::
* Table of Input Conversions::
* Numeric Input Conversions::
* String Input Conversions::
* Binary I/O::
* Temporary Files::
* EOF and Errors::
* File Positioning::
@end menu
@node Opening and Closing Files, Simple Output, C-Style I/O Functions, C-Style I/O Functions
@subsection Opening and Closing Files
@deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} fopen (@var{name}, @var{mode}, @var{arch})
@deftypefnx {Built-in Function} {@var{fid_list} =} fopen ("all")
@deftypefnx {Built-in Function} {@var{file} =} fopen (@var{fid})
The first form of the @code{fopen} function opens the named file with
the specified mode (read-write, read-only, etc.) and architecture
interpretation (IEEE big endian, IEEE little endian, etc.), and returns
an integer value that may be used to refer to the file later. If an
error occurs, @var{fid} is set to @minus{}1 and @var{msg} contains the
corresponding system error message. The @var{mode} is a one or two
character string that specifies whether the file is to be opened for
reading, writing, or both.
The second form of the @code{fopen} function returns a vector of file ids
corresponding to all the currently open files, excluding the
@code{stdin}, @code{stdout}, and @code{stderr} streams.
The third form of the @code{fopen} function returns the name of a
currently open file given its file id.
For example,
@example
myfile = fopen ("splat.dat", "r", "ieee-le");
@end example
@noindent
opens the file @file{splat.dat} for reading. If necessary, binary
numeric values will be read assuming they are stored in IEEE format with
the least significant bit first, and then converted to the native
representation.
Opening a file that is already open simply opens it again and returns a
separate file id. It is not an error to open a file several times,
though writing to the same file through several different file ids may
produce unexpected results.
The possible values @samp{mode} may have are
@table @asis
@item @samp{r}
Open a file for reading.
@item @samp{w}
Open a file for writing. The previous contents are discared.
@item @samp{a}
Open or create a file for writing at the end of the file.
@item @samp{r+}
Open an existing file for reading and writing.
@item @samp{w+}
Open a file for reading or writing. The previous contents are
discarded.
@item @samp{a+}
Open or create a file for reading or writing at the end of the
file.
@end table
The parameter @var{arch} is a string specifying the default data format
for the file. Valid values for @var{arch} are:
@table @asis
@samp{native}
The format of the current machine (this is the default).
@samp{ieee-le}
IEEE big endian format.
@samp{ieee-be}
IEEE little endian format.
@samp{vaxd}
VAX D floating format.
@samp{vaxg}
VAX G floating format.
@samp{cray}
Cray floating format.
@end table
@noindent
however, conversions are currently only supported for @samp{native}
@samp{ieee-be}, and @samp{ieee-le} formats.
@end deftypefn
@deftypefn {Built-in Function} {} fclose (@var{fid})
Closes the specified file. If an error is encountered while trying to
close the file, an error message is printed and @code{fclose} returns
0. Otherwise, it returns 1.
@end deftypefn
@node Simple Output, Line-Oriented Input, Opening and Closing Files, C-Style I/O Functions
@subsection Simple Output
@deftypefn {Built-in Function} {} fputs (@var{fid}, @var{string})
Write a string to a file with no formatting.
@end deftypefn
@deftypefn {Built-in Function} {} puts (@var{string})
Write a string to the standard output with no formatting.
@end deftypefn
@node Line-Oriented Input, Formatted Output, Simple Output, C-Style I/O Functions
@subsection Line-Oriented Input
@deftypefn {Built-in Function} {} fgetl (@var{fid}, @var{len})
Read characters from a file, stopping after a newline, or EOF,
or @var{len} characters have been read. The characters read, excluding
the possible trailing newline, are returned as a string.
If @var{len} is omitted, @code{fgetl} reads until the next newline
character.
If there are no more characters to read, @code{fgetl} returns @minus{}1.
@end deftypefn
@deftypefn {Built-in Function} {} fgets (@var{fid}, @var{len})
Read characters from a file, stopping after a newline, or EOF,
or @var{len} characters have been read. The characters read, including
the possible trailing newline, are returned as a string.
If @var{len} is omitted, @code{fgets} reads until the next newline
character.
If there are no more characters to read, @code{fgets} returns @minus{}1.
@end deftypefn
@node Formatted Output, Output Conversion for Matrices, Line-Oriented Input, C-Style I/O Functions
@subsection Formatted Output
This section describes how to call @code{printf} and related functions.
The following functions are available for formatted output. They are
modelled after the C language functions of the same name, but they
interpret the format template differently in order to improve the
performance of printing vector and matrix values.
@deftypefn {Function File} {} printf (@var{template}, @dots{})
The @code{printf} function prints the optional arguments under the
control of the template string @var{template} to the stream
@code{stdout}.
@end deftypefn
@deftypefn {Built-in Function} {} fprintf (@var{fid}, @var{template}, @dots{})
This function is just like @code{printf}, except that the output is
written to the stream @var{fid} instead of @code{stdout}.
@end deftypefn
@deftypefn {Built-in Function} {} sprintf (@var{template}, @dots{})
This is like @code{printf}, except that the output is returned as a
string. Unlike the C library function, which requires you to provide a
suitably sized string as an argument, Octave's @code{sprintf} function
returns the string, automatically sized to hold all of the items
converted.
@end deftypefn
The @code{printf} function can be used to print any number of arguments.
The template string argument you supply in a call provides
information not only about the number of additional arguments, but also
about their types and what style should be used for printing them.
Ordinary characters in the template string are simply written to the
output stream as-is, while @dfn{conversion specifications} introduced by
a @samp{%} character in the template cause subsequent arguments to be
formatted and written to the output stream. For example,
@cindex conversion specifications (@code{printf})
@smallexample
pct = 37;
filename = "foo.txt";
printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n",
filename, pct);
@end smallexample
@noindent
produces output like
@smallexample
Processing of `foo.txt' is 37% finished.
Please be patient.
@end smallexample
This example shows the use of the @samp{%d} conversion to specify that a
scalar argument should be printed in decimal notation, the @samp{%s}
conversion to specify printing of a string argument, and the @samp{%%}
conversion to print a literal @samp{%} character.
There are also conversions for printing an integer argument as an
unsigned value in octal, decimal, or hexadecimal radix (@samp{%o},
@samp{%u}, or @samp{%x}, respectively); or as a character value
(@samp{%c}).
Floating-point numbers can be printed in normal, fixed-point notation
using the @samp{%f} conversion or in exponential notation using the
@samp{%e} conversion. The @samp{%g} conversion uses either @samp{%e}
or @samp{%f} format, depending on what is more appropriate for the
magnitude of the particular number.
You can control formatting more precisely by writing @dfn{modifiers}
between the @samp{%} and the character that indicates which conversion
to apply. These slightly alter the ordinary behavior of the conversion.
For example, most conversion specifications permit you to specify a
minimum field width and a flag indicating whether you want the result
left- or right-justified within the field.
The specific flags and modifiers that are permitted and their
interpretation vary depending on the particular conversion. They're all
described in more detail in the following sections.
@node Output Conversion for Matrices, Output Conversion Syntax, Formatted Output, C-Style I/O Functions
@subsection Output Conversion for Matrices
When given a matrix value, Octave's formatted output functions cycle
through the format template until all the values in the matrix have been
printed. For example,
@example
@group
printf ("%4.2f %10.2e %8.4g\n", hilb (3));
@print{} 1.00 5.00e-01 0.3333
@print{} 0.50 3.33e-01 0.25
@print{} 0.33 2.50e-01 0.2
@end group
@end example
If more than one value is to be printed in a single call, the output
functions do not return to the beginning of the format template when
moving on from one value to the next. This can lead to confusing output
if the number of elements in the matrices are not exact multiples of the
number of conversions in the format template. For example,
@example
@group
printf ("%4.2f %10.2e %8.4g\n", [1, 2], [3, 4]);
@print{} 1.00 2.00e+00 3
@print{} 4.00
@end group
@end example
If this is not what you want, use a series of calls instead of just one.
@node Output Conversion Syntax, Table of Output Conversions, Output Conversion for Matrices, C-Style I/O Functions
@subsection Output Conversion Syntax
This section provides details about the precise syntax of conversion
specifications that can appear in a @code{printf} template
string.
Characters in the template string that are not part of a
conversion specification are printed as-is to the output stream.
The conversion specifications in a @code{printf} template string have
the general form:
@smallexample
% @var{flags} @var{width} @r{[} . @var{precision} @r{]} @var{type} @var{conversion}
@end smallexample
For example, in the conversion specifier @samp{%-10.8ld}, the @samp{-}
is a flag, @samp{10} specifies the field width, the precision is
@samp{8}, the letter @samp{l} is a type modifier, and @samp{d} specifies
the conversion style. (This particular type specifier says to print a
numeric argument in decimal notation, with a minimum of 8 digits
left-justified in a field at least 10 characters wide.)
In more detail, output conversion specifications consist of an
initial @samp{%} character followed in sequence by:
@itemize @bullet
@item
Zero or more @dfn{flag characters} that modify the normal behavior of
the conversion specification.
@cindex flag character (@code{printf})
@item
An optional decimal integer specifying the @dfn{minimum field width}.
If the normal conversion produces fewer characters than this, the field
is padded with spaces to the specified width. This is a @emph{minimum}
value; if the normal conversion produces more characters than this, the
field is @emph{not} truncated. Normally, the output is right-justified
within the field.
@cindex minimum field width (@code{printf})
You can also specify a field width of @samp{*}. This means that the
next argument in the argument list (before the actual value to be
printed) is used as the field width. The value is rounded to the
nearest integer. If the value is negative, this means to set the
@samp{-} flag (see below) and to use the absolute value as the field
width.
@item
An optional @dfn{precision} to specify the number of digits to be
written for the numeric conversions. If the precision is specified, it
consists of a period (@samp{.}) followed optionally by a decimal integer
(which defaults to zero if omitted).
@cindex precision (@code{printf})
You can also specify a precision of @samp{*}. This means that the next
argument in the argument list (before the actual value to be printed) is
used as the precision. The value must be an integer, and is ignored
if it is negative.
@item
An optional @dfn{type modifier character}. This character is ignored by
Octave's @code{printf} function, but is recognized to provide
compatibility with the C language @code{printf}.
@item
A character that specifies the conversion to be applied.
@end itemize
The exact options that are permitted and how they are interpreted vary
between the different conversion specifiers. See the descriptions of the
individual conversions for information about the particular options that
they use.
@node Table of Output Conversions, Integer Conversions, Output Conversion Syntax, C-Style I/O Functions
@subsection Table of Output Conversions
@cindex output conversions, for @code{printf}
Here is a table summarizing what all the different conversions do:
@table @asis
@item @samp{%d}, @samp{%i}
Print an integer as a signed decimal number. @xref{Integer
Conversions}, for details. @samp{%d} and @samp{%i} are synonymous for
output, but are different when used with @code{scanf} for input
(@pxref{Table of Input Conversions}).
@item @samp{%o}
Print an integer as an unsigned octal number. @xref{Integer
Conversions}, for details.
@item @samp{%u}
Print an integer as an unsigned decimal number. @xref{Integer
Conversions}, for details.
@item @samp{%x}, @samp{%X}
Print an integer as an unsigned hexadecimal number. @samp{%x} uses
lower-case letters and @samp{%X} uses upper-case. @xref{Integer
Conversions}, for details.
@item @samp{%f}
Print a floating-point number in normal (fixed-point) notation.
@xref{Floating-Point Conversions}, for details.
@item @samp{%e}, @samp{%E}
Print a floating-point number in exponential notation. @samp{%e} uses
lower-case letters and @samp{%E} uses upper-case. @xref{Floating-Point
Conversions}, for details.
@item @samp{%g}, @samp{%G}
Print a floating-point number in either normal (fixed-point) or
exponential notation, whichever is more appropriate for its magnitude.
@samp{%g} uses lower-case letters and @samp{%G} uses upper-case.
@xref{Floating-Point Conversions}, for details.
@item @samp{%c}
Print a single character. @xref{Other Output Conversions}.
@item @samp{%s}
Print a string. @xref{Other Output Conversions}.
@item @samp{%%}
Print a literal @samp{%} character. @xref{Other Output Conversions}.
@end table
If the syntax of a conversion specification is invalid, unpredictable
things will happen, so don't do this. If there aren't enough function
arguments provided to supply values for all the conversion
specifications in the template string, or if the arguments are not of
the correct types, the results are unpredictable. If you supply more
arguments than conversion specifications, the extra argument values are
simply ignored; this is sometimes useful.
@node Integer Conversions, Floating-Point Conversions, Table of Output Conversions, C-Style I/O Functions
@subsection Integer Conversions
This section describes the options for the @samp{%d}, @samp{%i},
@samp{%o}, @samp{%u}, @samp{%x}, and @samp{%X} conversion
specifications. These conversions print integers in various formats.
The @samp{%d} and @samp{%i} conversion specifications both print an
numeric argument as a signed decimal number; while @samp{%o},
@samp{%u}, and @samp{%x} print the argument as an unsigned octal,
decimal, or hexadecimal number (respectively). The @samp{%X} conversion
specification is just like @samp{%x} except that it uses the characters
@samp{ABCDEF} as digits instead of @samp{abcdef}.
The following flags are meaningful:
@table @asis
@item @samp{-}
Left-justify the result in the field (instead of the normal
right-justification).
@item @samp{+}
For the signed @samp{%d} and @samp{%i} conversions, print a
plus sign if the value is positive.
@item @samp{ }
For the signed @samp{%d} and @samp{%i} conversions, if the result
doesn't start with a plus or minus sign, prefix it with a space
character instead. Since the @samp{+} flag ensures that the result
includes a sign, this flag is ignored if you supply both of them.
@item @samp{#}
For the @samp{%o} conversion, this forces the leading digit to be
@samp{0}, as if by increasing the precision. For @samp{%x} or
@samp{%X}, this prefixes a leading @samp{0x} or @samp{0X} (respectively)
to the result. This doesn't do anything useful for the @samp{%d},
@samp{%i}, or @samp{%u} conversions.
@item @samp{0}
Pad the field with zeros instead of spaces. The zeros are placed after
any indication of sign or base. This flag is ignored if the @samp{-}
flag is also specified, or if a precision is specified.
@end table
If a precision is supplied, it specifies the minimum number of digits to
appear; leading zeros are produced if necessary. If you don't specify a
precision, the number is printed with as many digits as it needs. If
you convert a value of zero with an explicit precision of zero, then no
characters at all are produced.
@node Floating-Point Conversions, Other Output Conversions, Integer Conversions, C-Style I/O Functions
@subsection Floating-Point Conversions
This section discusses the conversion specifications for floating-point
numbers: the @samp{%f}, @samp{%e}, @samp{%E}, @samp{%g}, and @samp{%G}
conversions.
The @samp{%f} conversion prints its argument in fixed-point notation,
producing output of the form
@w{[@code{-}]@var{ddd}@code{.}@var{ddd}},
where the number of digits following the decimal point is controlled
by the precision you specify.
The @samp{%e} conversion prints its argument in exponential notation,
producing output of the form
@w{[@code{-}]@var{d}@code{.}@var{ddd}@code{e}[@code{+}|@code{-}]@var{dd}}.
Again, the number of digits following the decimal point is controlled by
the precision. The exponent always contains at least two digits. The
@samp{%E} conversion is similar but the exponent is marked with the letter
@samp{E} instead of @samp{e}.
The @samp{%g} and @samp{%G} conversions print the argument in the style
of @samp{%e} or @samp{%E} (respectively) if the exponent would be less
than -4 or greater than or equal to the precision; otherwise they use the
@samp{%f} style. Trailing zeros are removed from the fractional portion
of the result and a decimal-point character appears only if it is
followed by a digit.
The following flags can be used to modify the behavior:
@c Not @samp so we can have ` ' as an item.
@table @asis
@item @samp{-}
Left-justify the result in the field. Normally the result is
right-justified.
@item @samp{+}
Always include a plus or minus sign in the result.
@item @samp{ }
If the result doesn't start with a plus or minus sign, prefix it with a
space instead. Since the @samp{+} flag ensures that the result includes
a sign, this flag is ignored if you supply both of them.
@item @samp{#}
Specifies that the result should always include a decimal point, even
if no digits follow it. For the @samp{%g} and @samp{%G} conversions,
this also forces trailing zeros after the decimal point to be left
in place where they would otherwise be removed.
@item @samp{0}
Pad the field with zeros instead of spaces; the zeros are placed
after any sign. This flag is ignored if the @samp{-} flag is also
specified.
@end table
The precision specifies how many digits follow the decimal-point
character for the @samp{%f}, @samp{%e}, and @samp{%E} conversions. For
these conversions, the default precision is @code{6}. If the precision
is explicitly @code{0}, this suppresses the decimal point character
entirely. For the @samp{%g} and @samp{%G} conversions, the precision
specifies how many significant digits to print. Significant digits are
the first digit before the decimal point, and all the digits after it.
If the precision is @code{0} or not specified for @samp{%g} or
@samp{%G}, it is treated like a value of @code{1}. If the value being
printed cannot be expressed precisely in the specified number of digits,
the value is rounded to the nearest number that fits.
@node Other Output Conversions, Formatted Input, Floating-Point Conversions, C-Style I/O Functions
@subsection Other Output Conversions
This section describes miscellaneous conversions for @code{printf}.
The @samp{%c} conversion prints a single character. The @samp{-}
flag can be used to specify left-justification in the field, but no
other flags are defined, and no precision or type modifier can be given.
For example:
@smallexample
printf ("%c%c%c%c%c", "h", "e", "l", "l", "o");
@end smallexample
@noindent
prints @samp{hello}.
The @samp{%s} conversion prints a string. The corresponding argument
must be a string. A precision can be specified to indicate the maximum
number of characters to write; otherwise characters in the string up to
but not including the terminating null character are written to the
output stream. The @samp{-} flag can be used to specify
left-justification in the field, but no other flags or type modifiers
are defined for this conversion. For example:
@smallexample
printf ("%3s%-6s", "no", "where");
@end smallexample
@noindent
prints @samp{ nowhere } (note the leading and trailing spaces).
@node Formatted Input, Input Conversion Syntax, Other Output Conversions, C-Style I/O Functions
@subsection Formatted Input
Octave provides the @code{scanf}, @code{fscanf}, and @code{sscanf}
functions to read formatted input. There are two forms of each of these
functions. One can be used to extract vectors of data from a file, and
the other is more `C-like'.
@deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fscanf (@var{fid}, @var{template}, @var{size})
@deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } fscanf (@var{fid}, @var{template}, "C")
In the first form, read from @var{fid} according to @var{template},
returning the result in the matrix @var{val}.
The optional argument @var{size} specifies the amount of data to read
and may be one of
@table @code
@item Inf
Read as much as possible, returning a column vector.
@item @var{nr}
Read up to @var{nr} elements, returning a column vector.
@item [@var{nr}, Inf]
Read as much as possible, returning a matrix with @var{nr} rows. If the
number of elements read is not an exact multiple of @var{nr}, the last
column is padded with zeros.
@item [@var{nr}, @var{nc}]
Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with
@var{nr} rows. If the number of elements read is not an exact multiple
of @var{nr}, the last column is padded with zeros.
@end table
@noindent
If @var{size} is omitted, a value of @code{Inf} is assumed.
A string is returned if @var{template} specifies only character
conversions.
The number of items successfully read is returned in @var{count}.
In the second form, read from @var{fid} according to @var{template},
with each conversion specifier in @var{template} corresponding to a
single scalar return value. This form is more `C-like', and also
compatible with previous versions of Octave.
@end deftypefn
@deftypefn {Built-in Function} {[@var{val}, @var{count}] =} sscanf (@var{string}, @var{template}, @var{size})
@deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } sscanf (@var{string}, @var{template}, "C")
This is like @code{fscanf}, except that the characters are taken from the
string @var{string} instead of from a stream. Reaching the end of the
string is treated as an end-of-file condition.
@end deftypefn
@deftypefn {Built-in Function} {[@var{val}, @var{count}] =} scanf (@var{template}, @var{size})
@deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } scanf (@var{template}, "C")
This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.
It is currently not useful to call @code{scanf} in interactive
programs.
@end deftypefn
Calls to @code{scanf} are superficially similar to calls to
@code{printf} in that arbitrary arguments are read under the control of
a template string. While the syntax of the conversion specifications in
the template is very similar to that for @code{printf}, the
interpretation of the template is oriented more towards free-format
input and simple pattern matching, rather than fixed-field formatting.
For example, most @code{scanf} conversions skip over any amount of
``white space'' (including spaces, tabs, and newlines) in the input
file, and there is no concept of precision for the numeric input
conversions as there is for the corresponding output conversions.
Ordinarily, non-whitespace characters in the template are expected to
match characters in the input stream exactly.
@cindex conversion specifications (@code{scanf})
When a @dfn{matching failure} occurs, @code{scanf} returns immediately,
leaving the first non-matching character as the next character to be
read from the stream, and @code{scanf} returns all the items that were
successfully converted.
@cindex matching failure, in @code{scanf}
The formatted input functions are not used as frequently as the
formatted output functions. Partly, this is because it takes some care
to use them properly. Another reason is that it is difficult to recover
from a matching error.
@node Input Conversion Syntax, Table of Input Conversions, Formatted Input, C-Style I/O Functions
@subsection Input Conversion Syntax
A @code{scanf} template string is a string that contains ordinary
multibyte characters interspersed with conversion specifications that
start with @samp{%}.
Any whitespace character in the template causes any number of whitespace
characters in the input stream to be read and discarded. The whitespace
characters that are matched need not be exactly the same whitespace
characters that appear in the template string. For example, write
@samp{ , } in the template to recognize a comma with optional whitespace
before and after.
Other characters in the template string that are not part of conversion
specifications must match characters in the input stream exactly; if
this is not the case, a matching failure occurs.
The conversion specifications in a @code{scanf} template string
have the general form:
@smallexample
% @var{flags} @var{width} @var{type} @var{conversion}
@end smallexample
In more detail, an input conversion specification consists of an initial
@samp{%} character followed in sequence by:
@itemize @bullet
@item
An optional @dfn{flag character} @samp{*}, which says to ignore the text
read for this specification. When @code{scanf} finds a conversion
specification that uses this flag, it reads input as directed by the
rest of the conversion specification, but it discards this input, does
not return any valu, and does not increment the count of
successful assignments.
@cindex flag character (@code{scanf})
@item
An optional decimal integer that specifies the @dfn{maximum field
width}. Reading of characters from the input stream stops either when
this maximum is reached or when a non-matching character is found,
whichever happens first. Most conversions discard initial whitespace
characters, and these discarded characters don't count towards the
maximum field width. Conversions that do not discard initial whitespace
are explicitly documented.
@cindex maximum field width (@code{scanf})
@item
An optional type modifier character. This character is ignored by
Octave's @code{scanf} function, but is recognized to provide
compatibility with the C language @code{scanf}.
@item
A character that specifies the conversion to be applied.
@end itemize
The exact options that are permitted and how they are interpreted vary
between the different conversion specifiers. See the descriptions of the
individual conversions for information about the particular options that
they allow.
@node Table of Input Conversions, Numeric Input Conversions, Input Conversion Syntax, C-Style I/O Functions
@subsection Table of Input Conversions
@cindex input conversions, for @code{scanf}
Here is a table that summarizes the various conversion specifications:
@table @asis
@item @samp{%d}
Matches an optionally signed integer written in decimal. @xref{Numeric
Input Conversions}.
@item @samp{%i}
Matches an optionally signed integer in any of the formats that the C
language defines for specifying an integer constant. @xref{Numeric
Input Conversions}.
@item @samp{%o}
Matches an unsigned integer written in octal radix.
@xref{Numeric Input Conversions}.
@item @samp{%u}
Matches an unsigned integer written in decimal radix.
@xref{Numeric Input Conversions}.
@item @samp{%x}, @samp{%X}
Matches an unsigned integer written in hexadecimal radix.
@xref{Numeric Input Conversions}.
@item @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, @samp{%G}
Matches an optionally signed floating-point number. @xref{Numeric Input
Conversions}.
@item @samp{%s}
Matches a string containing only non-whitespace characters.
@xref{String Input Conversions}.
@item @samp{%c}
Matches a string of one or more characters; the number of characters
read is controlled by the maximum field width given for the conversion.
@xref{String Input Conversions}.
@item @samp{%%}
This matches a literal @samp{%} character in the input stream. No
corresponding argument is used.
@end table
If the syntax of a conversion specification is invalid, the behavior is
undefined. If there aren't enough function arguments provided to supply
addresses for all the conversion specifications in the template strings
that perform assignments, or if the arguments are not of the correct
types, the behavior is also undefined. On the other hand, extra
arguments are simply ignored.
@node Numeric Input Conversions, String Input Conversions, Table of Input Conversions, C-Style I/O Functions
@subsection Numeric Input Conversions
This section describes the @code{scanf} conversions for reading numeric
values.
The @samp{%d} conversion matches an optionally signed integer in decimal
radix.
The @samp{%i} conversion matches an optionally signed integer in any of
the formats that the C language defines for specifying an integer
constant.
For example, any of the strings @samp{10}, @samp{0xa}, or @samp{012}
could be read in as integers under the @samp{%i} conversion. Each of
these specifies a number with decimal value @code{10}.
The @samp{%o}, @samp{%u}, and @samp{%x} conversions match unsigned
integers in octal, decimal, and hexadecimal radices, respectively.
The @samp{%X} conversion is identical to the @samp{%x} conversion. They
both permit either uppercase or lowercase letters to be used as digits.
Unlike the C language @code{scanf}, Octave ignores the @samp{h},
@samp{l}, and @samp{L} modifiers.
@node String Input Conversions, Binary I/O, Numeric Input Conversions, C-Style I/O Functions
@subsection String Input Conversions
This section describes the @code{scanf} input conversions for reading
string and character values: @samp{%s} and @samp{%c}.
The @samp{%c} conversion is the simplest: it matches a fixed number of
characters, always. The maximum field with says how many characters to
read; if you don't specify the maximum, the default is 1. This
conversion does not skip over initial whitespace characters. It reads
precisely the next @var{n} characters, and fails if it cannot get that
many.
The @samp{%s} conversion matches a string of non-whitespace characters.
It skips and discards initial whitespace, but stops when it encounters
more whitespace after having read something.
For example, reading the input:
@smallexample
hello, world
@end smallexample
@noindent
with the conversion @samp{%10c} produces @code{" hello, wo"}, but
reading the same input with the conversion @samp{%10s} produces
@code{"hello,"}.
@node Binary I/O, Temporary Files, String Input Conversions, C-Style I/O Functions
@subsection Binary I/O
Octave can read and write binary data using the functions @code{fread}
and @code{fwrite}, which are patterned after the standard C functions
with the same names. The are able to automatically swap the byte order
of integer data and convert among ths supported floating point formats
as the data are read.
@deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip}, @var{arch})
Read binary data of type @var{precision} from the specified file ID
@var{fid}.
The optional argument @var{size} specifies the amount of data to read
and may be one of
@table @code
@item Inf
Read as much as possible, returning a column vector.
@item @var{nr}
Read up to @var{nr} elements, returning a column vector.
@item [@var{nr}, Inf]
Read as much as possible, returning a matrix with @var{nr} rows. If the
number of elements read is not an exact multiple of @var{nr}, the last
column is padded with zeros.
@item [@var{nr}, @var{nc}]
Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with
@var{nr} rows. If the number of elements read is not an exact multiple
of @var{nr}, the last column is padded with zeros.
@end table
@noindent
If @var{size} is omitted, a value of @code{Inf} is assumed.
The optional argument @var{precision} is a string specifying the type of
data to read and may be one of
@table @code
@item "char"
@itemx "char*1"
@itemx "integer*1"
@itemx "int8"
Single character.
@item "signed char"
@itemx "schar"
Signed character.
@item "unsigned char"
@itemx "uchar"
Unsigned character.
@item "short"
Short integer.
@item "unsigned short"
@itemx "ushort"
Unsigned short integer.
@item "int"
Integer.
@item "unsigned int"
@itemx "uint"
Unsigned integer.
@item "long"
Long integer.
@item "unsigned long"
@itemx "ulong"
Unsigned long integer.
@item "float"
@itemx "float32"
@itemx "real*4"
Single precision float.
@item "double"
@itemx "float64"
@itemx "real*8"
Double precision float.
@item "integer*2"
@itemx "int16"
Two byte integer.
@item "integer*4"
@itemx "int32"
Four byte integer.
@end table
@noindent
The default precision is @code{"uchar"}.
The optional argument @var{skip} specifies the number of bytes to skip
before each element is read. If it is not specified, a value of 0 is
assumed.
The optional argument @var{arch} is a string specifying the data format
for the file. Valid values are
@table @code
@item "native"
The format of the current machine.
@item "ieee-le"
IEEE big endian.
@item "ieee-be"
IEEE little endian.
@item "vaxd"
VAX D floating format.
@item "vaxg"
VAX G floating format.
@item "cray"
Cray floating format.
@end table
@noindent
Conversions are currently only supported for @code{"ieee-be"} and
@code{"ieee-le"} formats.
The data read from the file is returned in @var{val}, and the number of
values read is returned in @code{count}
@end deftypefn
@deftypefn {Built-in Function} {@var{count} =} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip}, @var{arch})
Write data in binary form of type @var{precision} to the specified file
ID @var{fid}, returning the number of values successfully written to the
file.
The argument @var{data} is a matrix of values that are to be written to
the file. The values are extracted in column-major order.
The remaining arguments @var{precision}, @var{skip}, and @var{arch} are
optional, and are interpreted as described for @code{fread}.
The behavior of @code{fwrite} is undefined if the values in @var{data}
are too large to fit in the specified precision.
@end deftypefn
@node Temporary Files, EOF and Errors, Binary I/O, C-Style I/O Functions
@subsection Temporary Files
@deftypefn {Built-in Function} {} tmpnam ()
Return a unique temporary file name as a string.
Since the named file is not opened, by @code{tmpnam}, it
is possible (though relatively unlikely) that it will not be available
by the time your program attempts to open it.
@end deftypefn
@node EOF and Errors, File Positioning, Temporary Files, C-Style I/O Functions
@subsection End of File and Errors
@deftypefn {Built-in Function} {} feof (@var{fid})
Return 1 if an end-of-file condition has been encountered for a given
file and 0 otherwise. Note that it will only return 1 if the end of the
file has already been encountered, not if the next read operation will
result in an end-of-file condition.
@end deftypefn
@deftypefn {Built-in Function} {} ferror (@var{fid})
Return 1 if an error condition has been encountered for a given file
and 0 otherwise. Note that it will only return 1 if an error has
already been encountered, not if the next operation will result in an
error condition.
@end deftypefn
@deftypefn {Built-in Function} {} freport ()
Print a list of which files have been opened, and whether they are open
for reading, writing, or both. For example,
@example
@group
freport ()
@print{} number mode name
@print{}
@print{} 0 r stdin
@print{} 1 w stdout
@print{} 2 w stderr
@print{} 3 r myfile
@end group
@end example
@end deftypefn
@node File Positioning, , EOF and Errors, C-Style I/O Functions
@subsection File Positioning
Three functions are available for setting and determining the position of
the file pointer for a given file.
@deftypefn {Built-in Function} {} ftell (@var{fid})
Return the position of the file pointer as the number of characters
from the beginning of the file @var{fid}.
@end deftypefn
@deftypefn {Built-in Function} {} fseek (@var{fid}, @var{offset}, @var{origin})
Set the file pointer to any location within the file @var{fid}. The
pointer is positioned @var{offset} characters from the @var{origin},
which may be one of the predefined variables @code{SEEK_CUR} (current
position), @code{SEEK_SET} (beginning), or @code{SEEK_END} (end of
file). If @var{origin} is omitted, @code{SEEK_SET} is assumed. The
offset must be zero, or a value returned by @code{ftell} (in which case
@var{origin} must be @code{SEEK_SET}.
@end deftypefn
@defvr {Built-in Variable} SEEK_SET
@defvrx {Built-in Variable} SEEK_CUR
@defvrx {Built-in Variable} SEEK_END
These variables may be used as the optional third argument for the
function @code{fseek}.
@end defvr
@deftypefn {Built-in Function} {} frewind (@var{fid})
Move the file pointer to the beginning of the file @var{fid}, returning
1 for success, and 0 if an error was encountered. It is equivalent to
@code{fseek (@var{fid}, 0, SEEK_SET)}.
@end deftypefn
The following example stores the current file position in the variable
@code{marker}, moves the pointer to the beginning of the file, reads
four characters, and then returns to the original position.
@example
marker = ftell (myfile);
frewind (myfile);
fourch = fgets (myfile, 4);
fseek (myfile, marker, SEEK_SET);
@end example
|