Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

fprintf - Formatted output to a file

&pagelevel(4)&pagelevel

Definition

#include <stdio.h>

int fprintf(FILE *fp, const char *format, argumentlist);

fprintf edits data (characters, strings, numeric values) according to the specifications in
the format string and writes this data to the file with file pointer fp.

fprintf works like printf, except that the edited data is written to a file and not to the
standard output.

Parameters

FILE *fp

File pointer to the output file.

const char *format

Format string as described under printf with KR or ANSI functionality (see printf).

argumentlist

Variables or constants whose values are to be converted and formatted for output
according to the information in the format statements.
If the number of format statements does not match the number of arguments the
following applies:
If there are more arguments, the surplus arguments are ignored.
If there are fewer arguments, the results are undefined.

Return val.

number of characters output

if successful.

Negative value if an error occurs.

Notes

fprintf rounds to the specified precision when converting floating-point numbers.

fprintf does not convert one data type to another. A value must be explicitly converted
(e.g. with the cast operator) if it is not to be output to conformity with its type.

The characters are not written immediately to the external file but are stored in an internal
C buffer (see section “Buffering” (Basic terms)).

Maximum number of characters to be output

With KR functionality (applies to C/C++ versions prior to V3.0 only) a maximum of 1400
characters can be output per fprintf call,
with ANSI functionality a maximum of 1400 characters per conversion element (e.g. %s).

Attempts to output non-initialized variables or to output variables in a manner inconsistent
with their data type can lead to undefined results.

The behavior is undefined if the percent sign (%) in a format statement is followed by an
undefined formatting or conversion character.

The following applies in the case of text files with SAM access mode and variable record
length for which a maximum record length is also specified: When the specification
split=no was entered for fopen, records which are longer than the maximum record
length are truncated to the maximum record length when they are written. By default or with
the specification split=yes, these records are split into multiple records. If a record has
precisely the maximum record length, a record of the length zero is written after it.

Example

#include <stdio.h>
#include <stdlib.h>
int main(void) 
{ 
    FILE *fp; 
    char c, name[40]; 
    int i; 
    char *string;
    double d; 
    printf("Name of the output file: \n"); 
    gets(name); 
    if((fp = fopen(name,"w")) == NULL)
    {
       printf("Can't open %s\n", name); 
       exit(1); 
    } 
    c = 'A'; 
    i = 999; 
    string = "This is a string.";
    d = 123.456; 
    fprintf(fp, "%c %d %s %f\n", c, i, string, d); 
    fclose(fp); 
    puts("Correct output to file:A 999 This is a string. 123.456000");
    return 0;
}

See also

printf, sprintf, snprintf, putc, putchar, puts, scanf, fscanf