Mercurial > octave
comparison doc/interpreter/io.txi @ 21633:dcf8922b724b
Deprecate printf, puts, and scanf. Make fputs a M-file.
* libinterp/corefcn/file-io.cc: extracted printf, puts, and scanf to M-files.
* scripts/deprecated/printf.m: new function, equivalent to version file-io.cc.
* scripts/deprecated/puts.m: new function, equivalent to version file-io.cc.
* scripts/deprecated/scanf.m: new function, equivalent to version file-io.cc.
* scripts/deprecated/module.mk: added printf, puts, and scanf.
* scripts/io/fputs.m: new function, equivalent to version file-io.cc.
* scripts/io/module.mk: added fputs.
* doc/interpreter/io.txi: adapted documentation to current implementation.
* NEWS: announced changes.
* doc/interpreter/basics.txi: replaced printf by fprintf.
* doc/interpreter/stmt.txi: replaced printf by fprintf.
* doc/interpreter/var.txi: replaced printf by fprintf.
* doc/refcard/refcard.tex: removed printf and scanf.
* examples/code/@FIRfilter/display.m: replaced printf by fprintf.
* examples/code/@polynomial/display.m: replaced printf by fprintf.
* scripts/@ftp/display.m: replaced printf by fprintf.
* scripts/general/inputParser.m: replaced printf by fprintf.
* scripts/general/methods.m: replaced printf by fprintf.
* scripts/general/profexplore.m: replaced printf by fprintf.
* scripts/general/profshow.m: replaced printf by fprintf.
* scripts/help/help.m: replaced puts by fputs and printf by fprintf.
* scripts/help/lookfor.m: replaced puts by fputs and printf by fprintf.
* scripts/help/which.m: replaced printf by fprintf.
* scripts/image/imformats.m: replaced printf by fprintf.
* scripts/io/beep.m: replaced puts by fputs.
* scripts/io/textread.m: replaced printf by fprintf.
* scripts/java/javaclasspath.m: replaced printf by fprintf.
* scripts/java/javamem.m: replaced printf by fprintf.
* scripts/miscellaneous/dir.m: replaced printf by fprintf.
* scripts/miscellaneous/dos.m: replaced printf by fprintf.
* scripts/miscellaneous/fact.m: replaced printf by fprintf.
* scripts/miscellaneous/info.m: replaced printf by fprintf.
* scripts/miscellaneous/license.m: replaced printf by fprintf.
* scripts/miscellaneous/ls.m: replaced puts by fputs.
* scripts/miscellaneous/menu.m: replaced printf by fprintf.
* scripts/miscellaneous/mkoctfile.m: replaced printf by fprintf.
* scripts/miscellaneous/private/display_info_file.m:
* scripts/miscellaneous/unix.m: replaced printf by fprintf.
* scripts/miscellaneous/ver.m: replaced printf by fprintf.
* scripts/miscellaneous/what.m: replaced printf by fprintf.
* scripts/ode/ode23.m: replaced printf by fprintf.
* scripts/ode/ode45.m: replaced printf by fprintf.
* scripts/optimization/fminbnd.m: replaced printf by fprintf.
* scripts/optimization/optimset.m: replaced puts by fputs and printf by fprintf.
* scripts/optimization/sqp.m: replaced printf by fprintf.
* scripts/pkg/pkg.m: replaced printf by fprintf.
* scripts/pkg/private/describe.m: replaced printf by fprintf.
* scripts/pkg/private/install.m: replaced printf by fprintf.
* scripts/pkg/private/installed_packages.m: replaced printf by fprintf.
* scripts/pkg/private/list_forge_packages.m: replaced puts by fputs and printf by fprintf.
* scripts/pkg/private/rebuild.m: replaced printf by fprintf.
* scripts/plot/util/__gnuplot_drawnow__.m: replaced puts by fputs and printf by fprintf.
* scripts/signal/stft.m: replaced printf by fprintf.
* scripts/sparse/bicg.m: replaced printf by fprintf.
* scripts/sparse/bicgstab.m: replaced printf by fprintf.
* scripts/sparse/cgs.m: replaced printf by fprintf.
* scripts/sparse/pcg.m: replaced printf by fprintf.
* scripts/sparse/pcr.m: replaced printf by fprintf.
* scripts/sparse/qmr.m: replaced printf by fprintf.
* scripts/statistics/models/logistic_regression.m: replaced printf by fprintf.
* scripts/statistics/tests/anova.m: replaced printf by fprintf.
* scripts/statistics/tests/bartlett_test.m: replaced printf by fprintf.
* scripts/statistics/tests/chisquare_test_homogeneity.m: replaced printf by fprintf.
* scripts/statistics/tests/chisquare_test_independence.m: replaced printf by fprintf.
* scripts/statistics/tests/cor_test.m: replaced printf by fprintf.
* scripts/statistics/tests/f_test_regression.m: replaced printf by fprintf.
* scripts/statistics/tests/hotelling_test.m: replaced printf by fprintf.
* scripts/statistics/tests/hotelling_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/kolmogorov_smirnov_test.m: replaced printf by fprintf.
* scripts/statistics/tests/kolmogorov_smirnov_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/kruskal_wallis_test.m: replaced printf by fprintf.
* scripts/statistics/tests/manova.m: replaced printf by fprintf.
* scripts/statistics/tests/mcnemar_test.m: replaced printf by fprintf.
* scripts/statistics/tests/prop_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/run_test.m: replaced printf by fprintf.
* scripts/statistics/tests/sign_test.m: replaced printf by fprintf.
* scripts/statistics/tests/t_test.m: replaced printf by fprintf.
* scripts/statistics/tests/t_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/t_test_regression.m: replaced printf by fprintf.
* scripts/statistics/tests/u_test.m: replaced printf by fprintf.
* scripts/statistics/tests/var_test.m: replaced printf by fprintf.
* scripts/statistics/tests/welch_test.m: replaced printf by fprintf.
* scripts/statistics/tests/wilcoxon_test.m: replaced printf by fprintf.
* scripts/statistics/tests/z_test.m: replaced printf by fprintf.
* scripts/statistics/tests/z_test_2.m: replaced printf by fprintf.
* scripts/strings/strtok.m: replaced printf by fprintf.
* scripts/testfun/__run_test_suite__.m: replaced puts by fputs and printf by fprintf.
* scripts/testfun/demo.m: replaced printf by fprintf.
* scripts/testfun/example.m: replaced printf by fprintf.
* scripts/testfun/private/compare_plot_demos.m: replaced printf by fprintf.
* scripts/testfun/rundemos.m: replaced printf by fprintf.
* scripts/testfun/runtests.m: replaced puts by fputs and printf by fprintf.
* scripts/testfun/speed.m: replaced printf by fprintf.
* scripts/testfun/test.m: replaced printf by fprintf.
* scripts/time/calendar.m: replaced puts by fputs and printf by fprintf.
* test/io.tst: replaced printf by fprintf.
author | Kai T. Ohlhus <k.ohlhus@gmail.com> |
---|---|
date | Wed, 20 Apr 2016 17:53:10 +0200 |
parents | 429f83903400 |
children | 96518f623c91 |
comparison
equal
deleted
inserted
replaced
21632:e3c44a120a8c | 21633:dcf8922b724b |
---|---|
86 @code{less} (and some versions of @code{more}) you can also scan forward | 86 @code{less} (and some versions of @code{more}) you can also scan forward |
87 and backward, and search for specific items. | 87 and backward, and search for specific items. |
88 | 88 |
89 Normally, no output is displayed by the pager until just before Octave | 89 Normally, no output is displayed by the pager until just before Octave |
90 is ready to print the top level prompt, or read from the standard input | 90 is ready to print the top level prompt, or read from the standard input |
91 (for example, by using the @code{fscanf} or @code{scanf} functions). | 91 (for example, by using the @code{fscanf} function). This means that there |
92 This means that there may be some delay before any output appears on | 92 may be some delay before any output appears on your screen if you have |
93 your screen if you have asked Octave to perform a significant amount of | 93 asked Octave to perform a significant amount of work with a single |
94 work with a single command statement. The function @code{fflush} may be | 94 command statement. The function @code{fflush} may be used to force output |
95 used to force output to be sent to the pager (or any other stream) | 95 to be sent to the pager (or any other stream) immediately. |
96 immediately. | |
97 | 96 |
98 You can select the program to run as the pager using the @env{PAGER} | 97 You can select the program to run as the pager using the @env{PAGER} |
99 function, and you can turn paging off by using the function | 98 function, and you can turn paging off by using the function |
100 @code{more}. | 99 @code{more}. |
101 | 100 |
331 @DOCSTRING(is_valid_file_id) | 330 @DOCSTRING(is_valid_file_id) |
332 | 331 |
333 @node Simple Output | 332 @node Simple Output |
334 @subsection Simple Output | 333 @subsection Simple Output |
335 | 334 |
336 Once a file has been opened for writing a string can be written to the | 335 The function @code{fputs} is the most simple way to write an unformatted |
337 file using the @code{fputs} function. The following example shows | 336 string to either @code{stdout} or to a file, that has been opened for |
338 how to write the string @samp{Free Software is needed for Free Science} | 337 writing. |
339 to the file @samp{free.txt}. | |
340 | |
341 @example | |
342 @group | |
343 filename = "free.txt"; | |
344 fid = fopen (filename, "w"); | |
345 fputs (fid, "Free Software is needed for Free Science"); | |
346 fclose (fid); | |
347 @end group | |
348 @end example | |
349 | 338 |
350 @DOCSTRING(fputs) | 339 @DOCSTRING(fputs) |
351 | |
352 A function much similar to @code{fputs} is available for writing data | |
353 to the screen. The @code{puts} function works just like @code{fputs} | |
354 except it doesn't take a file pointer as its input. | |
355 | |
356 @DOCSTRING(puts) | |
357 | 340 |
358 @node Line-Oriented Input | 341 @node Line-Oriented Input |
359 @subsection Line-Oriented Input | 342 @subsection Line-Oriented Input |
360 | 343 |
361 To read from a file it must be opened for reading using @code{fopen}. | 344 To read from a file it must be opened for reading using @code{fopen}. |
382 @DOCSTRING(fskipl) | 365 @DOCSTRING(fskipl) |
383 | 366 |
384 @node Formatted Output | 367 @node Formatted Output |
385 @subsection Formatted Output | 368 @subsection Formatted Output |
386 | 369 |
387 This section describes how to call @code{printf} and related functions. | 370 This section describes how to call the @code{fprintf} and @code{sprintf} |
388 | 371 functions. These functions are available for formatted output. They are |
389 The following functions are available for formatted output. They are | |
390 modeled after the C language functions of the same name, but they | 372 modeled after the C language functions of the same name, but they |
391 interpret the format template differently in order to improve the | 373 interpret the format template differently in order to improve the |
392 performance of printing vector and matrix values. | 374 performance of printing vector and matrix values. |
393 | 375 |
394 Implementation Note: For compatibility with @sc{matlab}, escape sequences in | |
395 the template string (e.g., @qcode{"@xbackslashchar{}n"} => newline) are | |
396 expanded even when the template string is defined with single quotes. | |
397 | |
398 @DOCSTRING(printf) | |
399 | |
400 @DOCSTRING(fprintf) | 376 @DOCSTRING(fprintf) |
401 | 377 |
402 @DOCSTRING(sprintf) | 378 @DOCSTRING(sprintf) |
403 | 379 |
404 The @code{printf} function can be used to print any number of arguments. | 380 @code{fprintf} and @code{sprintf} can be used to print any number of |
405 The template string argument you supply in a call provides | 381 arguments. The template string argument you supply in a call provides |
406 information not only about the number of additional arguments, but also | 382 information not only about the number of additional arguments, but also |
407 about their types and what style should be used for printing them. | 383 about their types and what style should be used for printing them. |
408 | 384 |
409 Ordinary characters in the template string are simply written to the | 385 Ordinary characters in the template string are simply written to the |
410 output stream as-is, while @dfn{conversion specifications} introduced by | 386 output stream as-is, while @dfn{conversion specifications} introduced by |
411 a @samp{%} character in the template cause subsequent arguments to be | 387 a @samp{%} character in the template cause subsequent arguments to be |
412 formatted and written to the output stream. For example, | 388 formatted and written to the output stream. For example, |
413 @cindex conversion specifications (@code{printf}) | 389 @cindex conversion specifications (@code{fprintf}) |
414 | 390 |
415 @example | 391 @example |
416 @group | 392 @group |
417 pct = 37; | 393 pct = 37; |
418 filename = "foo.txt"; | 394 filename = "foo.txt"; |
419 printf ("Processed %d%% of '%s'.\nPlease be patient.\n", | 395 fprintf ("Processed %d%% of '%s'.\nPlease be patient.\n", |
420 pct, filename); | 396 pct, filename); |
421 @end group | 397 @end group |
422 @end example | 398 @end example |
423 | 399 |
424 @noindent | 400 @noindent |
425 produces output like | 401 produces output like |
465 through the format template until all the values in the matrix have been | 441 through the format template until all the values in the matrix have been |
466 printed. For example: | 442 printed. For example: |
467 | 443 |
468 @example | 444 @example |
469 @group | 445 @group |
470 printf ("%4.2f %10.2e %8.4g\n", hilb (3)); | 446 fprintf ("%4.2f %10.2e %8.4g\n", hilb (3)); |
471 | 447 |
472 @print{} 1.00 5.00e-01 0.3333 | 448 @print{} 1.00 5.00e-01 0.3333 |
473 @print{} 0.50 3.33e-01 0.25 | 449 @print{} 0.50 3.33e-01 0.25 |
474 @print{} 0.33 2.50e-01 0.2 | 450 @print{} 0.33 2.50e-01 0.2 |
475 @end group | 451 @end group |
481 if the number of elements in the matrices are not exact multiples of the | 457 if the number of elements in the matrices are not exact multiples of the |
482 number of conversions in the format template. For example: | 458 number of conversions in the format template. For example: |
483 | 459 |
484 @example | 460 @example |
485 @group | 461 @group |
486 printf ("%4.2f %10.2e %8.4g\n", [1, 2], [3, 4]); | 462 fprintf ("%4.2f %10.2e %8.4g\n", [1, 2], [3, 4]); |
487 | 463 |
488 @print{} 1.00 2.00e+00 3 | 464 @print{} 1.00 2.00e+00 3 |
489 @print{} 4.00 | 465 @print{} 4.00 |
490 @end group | 466 @end group |
491 @end example | 467 @end example |
494 | 470 |
495 @node Output Conversion Syntax | 471 @node Output Conversion Syntax |
496 @subsection Output Conversion Syntax | 472 @subsection Output Conversion Syntax |
497 | 473 |
498 This section provides details about the precise syntax of conversion | 474 This section provides details about the precise syntax of conversion |
499 specifications that can appear in a @code{printf} template | 475 specifications that can appear in a @code{fprintf} or @code{sprintf} |
500 string. | 476 template string. |
501 | 477 |
502 Characters in the template string that are not part of a | 478 Characters in the template string that are not part of a |
503 conversion specification are printed as-is to the output stream. | 479 conversion specification are printed as-is to the output stream. |
504 | 480 |
505 The conversion specifications in a @code{printf} template string have | 481 The conversion specifications in a template string have the general form: |
506 the general form: | |
507 | 482 |
508 @example | 483 @example |
509 % @var{flags} @var{width} @r{[} . @var{precision} @r{]} @var{type} @var{conversion} | 484 % @var{flags} @var{width} @r{[} . @var{precision} @r{]} @var{type} @var{conversion} |
510 @end example | 485 @end example |
511 | 486 |
521 | 496 |
522 @itemize @bullet | 497 @itemize @bullet |
523 @item | 498 @item |
524 Zero or more @dfn{flag characters} that modify the normal behavior of | 499 Zero or more @dfn{flag characters} that modify the normal behavior of |
525 the conversion specification. | 500 the conversion specification. |
526 @cindex flag character (@code{printf}) | 501 @cindex flag character (@code{fprintf}) |
527 | 502 |
528 @item | 503 @item |
529 An optional decimal integer specifying the @dfn{minimum field width}. | 504 An optional decimal integer specifying the @dfn{minimum field width}. |
530 If the normal conversion produces fewer characters than this, the field | 505 If the normal conversion produces fewer characters than this, the field |
531 is padded with spaces to the specified width. This is a @emph{minimum} | 506 is padded with spaces to the specified width. This is a @emph{minimum} |
532 value; if the normal conversion produces more characters than this, the | 507 value; if the normal conversion produces more characters than this, the |
533 field is @emph{not} truncated. Normally, the output is right-justified | 508 field is @emph{not} truncated. Normally, the output is right-justified |
534 within the field. | 509 within the field. |
535 @cindex minimum field width (@code{printf}) | 510 @cindex minimum field width (@code{fprintf}) |
536 | 511 |
537 You can also specify a field width of @samp{*}. This means that the | 512 You can also specify a field width of @samp{*}. This means that the |
538 next argument in the argument list (before the actual value to be | 513 next argument in the argument list (before the actual value to be |
539 printed) is used as the field width. The value is rounded to the | 514 printed) is used as the field width. The value is rounded to the |
540 nearest integer. If the value is negative, this means to set the | 515 nearest integer. If the value is negative, this means to set the |
544 @item | 519 @item |
545 An optional @dfn{precision} to specify the number of digits to be | 520 An optional @dfn{precision} to specify the number of digits to be |
546 written for the numeric conversions. If the precision is specified, it | 521 written for the numeric conversions. If the precision is specified, it |
547 consists of a period (@samp{.}) followed optionally by a decimal integer | 522 consists of a period (@samp{.}) followed optionally by a decimal integer |
548 (which defaults to zero if omitted). | 523 (which defaults to zero if omitted). |
549 @cindex precision (@code{printf}) | 524 @cindex precision (@code{fprintf}) |
550 | 525 |
551 You can also specify a precision of @samp{*}. This means that the next | 526 You can also specify a precision of @samp{*}. This means that the next |
552 argument in the argument list (before the actual value to be printed) is | 527 argument in the argument list (before the actual value to be printed) is |
553 used as the precision. The value must be an integer, and is ignored | 528 used as the precision. The value must be an integer, and is ignored |
554 if it is negative. | 529 if it is negative. |
555 | 530 |
556 @item | 531 @item |
557 An optional @dfn{type modifier character}. This character is ignored by | 532 An optional @dfn{type modifier character}. This character is ignored by |
558 Octave's @code{printf} function, but is recognized to provide | 533 Octave's @code{fprintf} function, but is recognized to provide |
559 compatibility with the C language @code{printf}. | 534 compatibility with the C language @code{fprintf}. |
560 | 535 |
561 @item | 536 @item |
562 A character that specifies the conversion to be applied. | 537 A character that specifies the conversion to be applied. |
563 @end itemize | 538 @end itemize |
564 | 539 |
567 individual conversions for information about the particular options that | 542 individual conversions for information about the particular options that |
568 they use. | 543 they use. |
569 | 544 |
570 @node Table of Output Conversions | 545 @node Table of Output Conversions |
571 @subsection Table of Output Conversions | 546 @subsection Table of Output Conversions |
572 @cindex output conversions, for @code{printf} | 547 @cindex output conversions, for @code{fprintf} |
573 | 548 |
574 Here is a table summarizing what all the different conversions do: | 549 Here is a table summarizing what all the different conversions do: |
575 | 550 |
576 @table @asis | 551 @table @asis |
577 @item @samp{%d}, @samp{%i} | 552 @item @samp{%d}, @samp{%i} |
578 Print an integer as a signed decimal number. @xref{Integer | 553 Print an integer as a signed decimal number. @xref{Integer |
579 Conversions}, for details. @samp{%d} and @samp{%i} are synonymous for | 554 Conversions}, for details. @samp{%d} and @samp{%i} are synonymous for |
580 output, but are different when used with @code{scanf} for input | 555 output, but are different when used with @code{fscanf} for input |
581 (@pxref{Table of Input Conversions}). | 556 (@pxref{Table of Input Conversions}). |
582 | 557 |
583 @item @samp{%o} | 558 @item @samp{%o} |
584 Print an integer as an unsigned octal number. @xref{Integer | 559 Print an integer as an unsigned octal number. @xref{Integer |
585 Conversions}, for details. | 560 Conversions}, for details. |
621 If the syntax of a conversion specification is invalid, unpredictable | 596 If the syntax of a conversion specification is invalid, unpredictable |
622 things will happen, so don't do this. In particular, @sc{matlab} allows | 597 things will happen, so don't do this. In particular, @sc{matlab} allows |
623 a bare percentage sign @samp{%} with no subsequent conversion character. | 598 a bare percentage sign @samp{%} with no subsequent conversion character. |
624 Octave will emit an error and stop if it sees such code. When the string | 599 Octave will emit an error and stop if it sees such code. When the string |
625 variable to be processed cannot be guaranteed to be free of potential format | 600 variable to be processed cannot be guaranteed to be free of potential format |
626 codes it is better to use the two argument form of any of the @code{printf} | 601 codes it is better to use the two argument form of @code{fprintf} and |
627 functions and set the format string to @code{%s}. Alternatively, for code | 602 @code{sprintf} and set the format string to @code{%s}. Alternatively, |
628 which is not required to be backwards-compatible with @sc{matlab} the | 603 the function @code{disp} can be used. |
629 Octave function @code{puts} or @code{disp} can be used. | 604 |
630 | 605 @example |
631 @example | 606 @group |
632 @group | 607 fprintf (strvar); # Unsafe if strvar contains format codes |
633 printf (strvar); # Unsafe if strvar contains format codes | 608 fprintf ("%s", strvar); # Safe |
634 printf ("%s", strvar); # Safe | 609 disp (strvar); # Safe |
635 puts (strvar); # Safe | |
636 @end group | 610 @end group |
637 @end example | 611 @end example |
638 | 612 |
639 If there aren't enough function arguments provided to supply values for all | 613 If there aren't enough function arguments provided to supply values for all |
640 the conversion specifications in the template string, or if the arguments are | 614 the conversion specifications in the template string, or if the arguments are |
761 the value is rounded to the nearest number that fits. | 735 the value is rounded to the nearest number that fits. |
762 | 736 |
763 @node Other Output Conversions | 737 @node Other Output Conversions |
764 @subsection Other Output Conversions | 738 @subsection Other Output Conversions |
765 | 739 |
766 This section describes miscellaneous conversions for @code{printf}. | 740 This section describes miscellaneous conversions for @code{fprintf} and |
741 @code{sprintf}. | |
767 | 742 |
768 The @samp{%c} conversion prints a single character. The @samp{-} | 743 The @samp{%c} conversion prints a single character. The @samp{-} |
769 flag can be used to specify left-justification in the field, but no | 744 flag can be used to specify left-justification in the field, but no |
770 other flags are defined, and no precision or type modifier can be given. | 745 other flags are defined, and no precision or type modifier can be given. |
771 For example: | 746 For example: |
772 | 747 |
773 @example | 748 @example |
774 printf ("%c%c%c%c%c", "h", "e", "l", "l", "o"); | 749 fprintf ("%c%c%c%c%c", "h", "e", "l", "l", "o"); |
775 @end example | 750 @end example |
776 | 751 |
777 @noindent | 752 @noindent |
778 prints @samp{hello}. | 753 prints @samp{hello}. |
779 | 754 |
784 output stream. The @samp{-} flag can be used to specify | 759 output stream. The @samp{-} flag can be used to specify |
785 left-justification in the field, but no other flags or type modifiers | 760 left-justification in the field, but no other flags or type modifiers |
786 are defined for this conversion. For example: | 761 are defined for this conversion. For example: |
787 | 762 |
788 @example | 763 @example |
789 printf ("%3s%-6s", "no", "where"); | 764 fprintf ("%3s%-6s", "no", "where"); |
790 @end example | 765 @end example |
791 | 766 |
792 @noindent | 767 @noindent |
793 prints @samp{ nowhere } (note the leading and trailing spaces). | 768 prints @samp{ nowhere } (note the leading and trailing spaces). |
794 | 769 |
795 @node Formatted Input | 770 @node Formatted Input |
796 @subsection Formatted Input | 771 @subsection Formatted Input |
797 | 772 |
798 Octave provides the @code{scanf}, @code{fscanf}, and @code{sscanf} | 773 Octave provides the @code{fscanf} and @code{sscanf} functions to read |
799 functions to read formatted input. There are two forms of each of these | 774 formatted input. There are two forms of each of these functions. One |
800 functions. One can be used to extract vectors of data from a file, and | 775 can be used to extract vectors of data from a file, and the other is |
801 the other is more `C-like'. | 776 more `C-like'. |
802 | 777 |
803 @DOCSTRING(fscanf) | 778 @DOCSTRING(fscanf) |
804 | 779 |
805 @DOCSTRING(scanf) | |
806 | |
807 @DOCSTRING(sscanf) | 780 @DOCSTRING(sscanf) |
808 | 781 |
809 Calls to @code{scanf} are superficially similar to calls to | 782 Calls to @code{fscanf} are superficially similar to calls to |
810 @code{printf} in that arbitrary arguments are read under the control of | 783 @code{fprintf} in that arbitrary arguments are read under the control of |
811 a template string. While the syntax of the conversion specifications in | 784 a template string. While the syntax of the conversion specifications in |
812 the template is very similar to that for @code{printf}, the | 785 the template is very similar to that for @code{fprintf}, the |
813 interpretation of the template is oriented more towards free-format | 786 interpretation of the template is oriented more towards free-format |
814 input and simple pattern matching, rather than fixed-field formatting. | 787 input and simple pattern matching, rather than fixed-field formatting. |
815 For example, most @code{scanf} conversions skip over any amount of | 788 For example, most @code{fscanf} conversions skip over any amount of |
816 ``white space'' (including spaces, tabs, and newlines) in the input | 789 ``white space'' (including spaces, tabs, and newlines) in the input |
817 file, and there is no concept of precision for the numeric input | 790 file, and there is no concept of precision for the numeric input |
818 conversions as there is for the corresponding output conversions. | 791 conversions as there is for the corresponding output conversions. |
819 Ordinarily, non-whitespace characters in the template are expected to | 792 Ordinarily, non-whitespace characters in the template are expected to |
820 match characters in the input stream exactly. | 793 match characters in the input stream exactly. |
821 @cindex conversion specifications (@code{scanf}) | 794 @cindex conversion specifications (@code{fscanf}) |
822 | 795 |
823 When a @dfn{matching failure} occurs, @code{scanf} returns immediately, | 796 When a @dfn{matching failure} occurs, @code{fscanf} returns immediately, |
824 leaving the first non-matching character as the next character to be | 797 leaving the first non-matching character as the next character to be |
825 read from the stream, and @code{scanf} returns all the items that were | 798 read from the stream, and @code{fscanf} returns all the items that were |
826 successfully converted. | 799 successfully converted. |
827 @cindex matching failure, in @code{scanf} | 800 @cindex matching failure, in @code{fscanf} |
828 | 801 |
829 The formatted input functions are not used as frequently as the | 802 The formatted input functions are not used as frequently as the |
830 formatted output functions. Partly, this is because it takes some care | 803 formatted output functions. Partly, this is because it takes some care |
831 to use them properly. Another reason is that it is difficult to recover | 804 to use them properly. Another reason is that it is difficult to recover |
832 from a matching error. | 805 from a matching error. |
833 | 806 |
834 @node Input Conversion Syntax | 807 @node Input Conversion Syntax |
835 @subsection Input Conversion Syntax | 808 @subsection Input Conversion Syntax |
836 | 809 |
837 A @code{scanf} template string is a string that contains ordinary | 810 A @code{fscanf} template string is a string that contains ordinary |
838 multibyte characters interspersed with conversion specifications that | 811 multibyte characters interspersed with conversion specifications that |
839 start with @samp{%}. | 812 start with @samp{%}. |
840 | 813 |
841 Any whitespace character in the template causes any number of whitespace | 814 Any whitespace character in the template causes any number of whitespace |
842 characters in the input stream to be read and discarded. The whitespace | 815 characters in the input stream to be read and discarded. The whitespace |
847 | 820 |
848 Other characters in the template string that are not part of conversion | 821 Other characters in the template string that are not part of conversion |
849 specifications must match characters in the input stream exactly; if | 822 specifications must match characters in the input stream exactly; if |
850 this is not the case, a matching failure occurs. | 823 this is not the case, a matching failure occurs. |
851 | 824 |
852 The conversion specifications in a @code{scanf} template string | 825 The conversion specifications in a @code{fscanf} template string |
853 have the general form: | 826 have the general form: |
854 | 827 |
855 @example | 828 @example |
856 % @var{flags} @var{width} @var{type} @var{conversion} | 829 % @var{flags} @var{width} @var{type} @var{conversion} |
857 @end example | 830 @end example |
860 @samp{%} character followed in sequence by: | 833 @samp{%} character followed in sequence by: |
861 | 834 |
862 @itemize @bullet | 835 @itemize @bullet |
863 @item | 836 @item |
864 An optional @dfn{flag character} @samp{*}, which says to ignore the text | 837 An optional @dfn{flag character} @samp{*}, which says to ignore the text |
865 read for this specification. When @code{scanf} finds a conversion | 838 read for this specification. When @code{fscanf} finds a conversion |
866 specification that uses this flag, it reads input as directed by the | 839 specification that uses this flag, it reads input as directed by the |
867 rest of the conversion specification, but it discards this input, does | 840 rest of the conversion specification, but it discards this input, does |
868 not return any value, and does not increment the count of | 841 not return any value, and does not increment the count of |
869 successful assignments. | 842 successful assignments. |
870 @cindex flag character (@code{scanf}) | 843 @cindex flag character (@code{fscanf}) |
871 | 844 |
872 @item | 845 @item |
873 An optional decimal integer that specifies the @dfn{maximum field | 846 An optional decimal integer that specifies the @dfn{maximum field |
874 width}. Reading of characters from the input stream stops either when | 847 width}. Reading of characters from the input stream stops either when |
875 this maximum is reached or when a non-matching character is found, | 848 this maximum is reached or when a non-matching character is found, |
876 whichever happens first. Most conversions discard initial whitespace | 849 whichever happens first. Most conversions discard initial whitespace |
877 characters, and these discarded characters don't count towards the | 850 characters, and these discarded characters don't count towards the |
878 maximum field width. Conversions that do not discard initial whitespace | 851 maximum field width. Conversions that do not discard initial whitespace |
879 are explicitly documented. | 852 are explicitly documented. |
880 @cindex maximum field width (@code{scanf}) | 853 @cindex maximum field width (@code{fscanf}) |
881 | 854 |
882 @item | 855 @item |
883 An optional type modifier character. This character is ignored by | 856 An optional type modifier character. This character is ignored by |
884 Octave's @code{scanf} function, but is recognized to provide | 857 Octave's @code{fscanf} function, but is recognized to provide |
885 compatibility with the C language @code{scanf}. | 858 compatibility with the C language @code{fscanf}. |
886 | 859 |
887 @item | 860 @item |
888 A character that specifies the conversion to be applied. | 861 A character that specifies the conversion to be applied. |
889 @end itemize | 862 @end itemize |
890 | 863 |
893 individual conversions for information about the particular options that | 866 individual conversions for information about the particular options that |
894 they allow. | 867 they allow. |
895 | 868 |
896 @node Table of Input Conversions | 869 @node Table of Input Conversions |
897 @subsection Table of Input Conversions | 870 @subsection Table of Input Conversions |
898 @cindex input conversions, for @code{scanf} | 871 @cindex input conversions, for @code{fscanf} |
899 | 872 |
900 Here is a table that summarizes the various conversion specifications: | 873 Here is a table that summarizes the various conversion specifications: |
901 | 874 |
902 @table @asis | 875 @table @asis |
903 @item @samp{%d} | 876 @item @samp{%d} |
947 arguments are simply ignored. | 920 arguments are simply ignored. |
948 | 921 |
949 @node Numeric Input Conversions | 922 @node Numeric Input Conversions |
950 @subsection Numeric Input Conversions | 923 @subsection Numeric Input Conversions |
951 | 924 |
952 This section describes the @code{scanf} conversions for reading numeric | 925 This section describes the @code{fscanf} conversions for reading numeric |
953 values. | 926 values. |
954 | 927 |
955 The @samp{%d} conversion matches an optionally signed integer in decimal | 928 The @samp{%d} conversion matches an optionally signed integer in decimal |
956 radix. | 929 radix. |
957 | 930 |
967 integers in octal, decimal, and hexadecimal radices, respectively. | 940 integers in octal, decimal, and hexadecimal radices, respectively. |
968 | 941 |
969 The @samp{%X} conversion is identical to the @samp{%x} conversion. They | 942 The @samp{%X} conversion is identical to the @samp{%x} conversion. They |
970 both permit either uppercase or lowercase letters to be used as digits. | 943 both permit either uppercase or lowercase letters to be used as digits. |
971 | 944 |
972 Unlike the C language @code{scanf}, Octave ignores the @samp{h}, | 945 Unlike the C language @code{fscanf}, Octave ignores the @samp{h}, |
973 @samp{l}, and @samp{L} modifiers. | 946 @samp{l}, and @samp{L} modifiers. |
974 | 947 |
975 @node String Input Conversions | 948 @node String Input Conversions |
976 @subsection String Input Conversions | 949 @subsection String Input Conversions |
977 | 950 |
978 This section describes the @code{scanf} input conversions for reading | 951 This section describes the @code{fscanf} input conversions for reading |
979 string and character values: @samp{%s} and @samp{%c}. | 952 string and character values: @samp{%s} and @samp{%c}. |
980 | 953 |
981 The @samp{%c} conversion is the simplest: it matches a fixed number of | 954 The @samp{%c} conversion is the simplest: it matches a fixed number of |
982 characters, always. The maximum field with says how many characters to | 955 characters, always. The maximum field with says how many characters to |
983 read; if you don't specify the maximum, the default is 1. This | 956 read; if you don't specify the maximum, the default is 1. This |
1089 frewind (myfile); | 1062 frewind (myfile); |
1090 fourch = fgets (myfile, 4); | 1063 fourch = fgets (myfile, 4); |
1091 fseek (myfile, marker, SEEK_SET); | 1064 fseek (myfile, marker, SEEK_SET); |
1092 @end group | 1065 @end group |
1093 @end example | 1066 @end example |
1094 |