fprintf

Parent Previous Next

fprintf

y = fprintf ( file,format,... )
y = file.fprintf ( format,... )

Write formatted data to a file

arg

type

Description

Unit or Value scale

file

TXT or SCAL

file name string or file identifier


format

TXT

Formatted text to write to the file, following the same convention as in C language.


...

...

n/a


Outputs

  • y is the number of characters written. If an error occurs, y is negative and one of the following:
    -1: fopen error
    -2: invalid file identifier
    -3: fwrite error
    -4: fclose error
    -999: Unknown error

Notes

  • if file is zero, the content is sent to the console output.
  • file can be either a text of the file name (if an extension is not specified, .txt is added by default) or a file identifier, which is the output of fopen
  • When the file name is specified for file, fprintf opens the file with the mode "at," meaning that the content will be appended to the existing content in the file, writes the content as specified, and closes the file.
  • If a file identifier is used for file, fprintf writes the content to the file stream opened by a prior call to fopen.

Examples

  • To open the file (if file is not found, create one) and write an integer val at the end of the file log.txt; if the file is not found, create one:
    AUX> fprintf("log.txt","%d\n", val)
  • Integers from 1 through 10 are summed consecutively. To create a file val to the file log.txt, and write each integer and the cumulative sum:
    AUX> sum=0; fd=fopen("log.txt","wt); for k=1:10, sum += k; fprintf(fd,"%d %d\n", k, sum); end, fclose(fd)
  • To make a formatted string output and show it on the console (this is useful inside a UDF):
    AUX> fprintf(0,"%d\n", val)

See Also

fopen | fclose | sprintf

Created with the Personal Edition of HelpNDoc: Free Web Help generator