fprintf
Write formatted data to a file
Syntax
y = fprintf ( file,format,... )
y = file.fprintf ( format,... )
Inputs
| Argument | Type | Description | Unit |
|---|---|---|---|
file | text or scalar | file name string or file identifier | n/a |
format | text | Formatted text to write to the file, following the same convention as in C language. | - |
... | ... | n/a | - |
Outputs / Return Value
y:yis the number of characters written. If an error occurs,yis negative and one of the following: -1: fopen error -2: invalid file identifier -3: fwrite error -4: fclose error -999: Unknown error
Notes
- if
fileis zero, the content is sent to the console output. filecan 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
Review Status
- Legacy parameter naming does not exactly match current engine metadata and should be reviewed manually.