Writing to Files
Character Output Functions
The fputc function
#include <stdio.h>
int fputc(int c, FILE *stream);
The fputc function writes the character specified by c (converted to an unsigned char) to the stream pointed to by stream at the position indicated by the associated file position indicator (if defined), and advances the indicator appropriately. If the file cannot support positioning requests, or if the stream is opened with append mode, the character is appended to the output stream. The
function returns the character written, unless a write error occurs, in which case the error indicator for the stream is set and fputc returns EOF.
function returns the character written, unless a write error occurs, in which case the error indicator for the stream is set and fputc returns EOF.
The fputs function
#include <stdio.h>
int fputs(const char *s, FILE *stream);
The fputs function writes the string pointed to by s to the stream pointed to by stream. The
terminating null character is not written. The function returns EOF if a write error occurs, otherwise it returns a nonnegative value.
The putc function
#include <stdio.h>
int putc(int c, FILE *stream);
The putc function is equivalent to fputc, except that if it is implemented as a macro, it may
evaluate stream more than once, so the argument should never be an expression with side effects. The function returns the character written, unless a write error occurs, in which case the error
indicator for the stream is set and the function returns EOF.
indicator for the stream is set and the function returns EOF.
The putchar function
#include <stdio.h>
int putchar(int c);
int putchar(int c);
The putchar function is equivalent to putc with the second argument stdout. It returns the character written, unless a write error occurs, in which case the error indicator for stdout is set and the function returns EOF.
The puts function
#include <stdio.h>
int puts(const char *s);
The puts function writes the string pointed to by s to the stream pointed to by stdout, and
appends a newline character to the output. The terminating null character is not written. The function returns EOF if a write error occurs; otherwise, it returns a nonnegative value.
Direct output function: the fwrite function
#include <stdio.h>
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
The fwrite function writes, from the array pointed to by ptr, up to nmemb elements whose size
is specified by size to the stream pointed to by stream. The file position indicator for the stream
(if defined) is advanced by the number of characters successfully written. If an error occurs, the
resulting value of the file position indicator for the stream is indeterminate. The function returns the
number of elements successfully written, which will be less than nmemb only if a write error is
encountered.
is specified by size to the stream pointed to by stream. The file position indicator for the stream
(if defined) is advanced by the number of characters successfully written. If an error occurs, the
resulting value of the file position indicator for the stream is indeterminate. The function returns the
number of elements successfully written, which will be less than nmemb only if a write error is
encountered.
Formatted output functions: the printf family of functions
#include <stdarg.h>
#include <stdio.h>
int fprintf(FILE *stream, const char *format, ...); int printf(const char *format, ...);
int sprintf(char *s, const char *format, ...);
int vfprintf(FILE *stream, const char *format, va_list arg);
int vprintf(const char *format, va_list arg);
int vsprintf(char *s, const char *format, va_list arg);
int vprintf(const char *format, va_list arg);
int vsprintf(char *s, const char *format, va_list arg);
Note: Some length specifiers and format specifiers are new in C99. These may not be available in older compilers and versions of the stdio library, which adhere to the C89/C90 standard. Wherever possible, the new ones will be marked with (C99).
The fprintf function writes output to the stream pointed to by stream under control of the
string pointed to by format that specifies how subsequent arguments are converted for output. If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored. The fprintf function returns when the end of the format string is encountered.
The format shall be a multibyte character sequence, beginning and ending in its initial shift state.
The format is composed of zero or more directives: ordinary multibyte characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in
fetching zero or more subsequent arguments, converting them, if applicable, according to the
corresponding conversion specifier, and then writing the result to the output stream.
fetching zero or more subsequent arguments, converting them, if applicable, according to the
corresponding conversion specifier, and then writing the result to the output stream.
Each conversion specification is introduced by the character %. After the %, the following appear in sequence:
•
Zero or more flags (in any order) that modify the meaning of the conversion specification.
• An optional minimum field width. If the converted value has fewer characters than the field
width, it is padded with spaces (by default) on the left (or right, if the left adjustment flag,
described later, has been given) to the field width. The field width takes the form of an
asterisk * (described later) or a decimal integer. (Note that 0 is taken as a flag, not as the
beginning of a field width.)
width, it is padded with spaces (by default) on the left (or right, if the left adjustment flag,
described later, has been given) to the field width. The field width takes the form of an
asterisk * (described later) or a decimal integer. (Note that 0 is taken as a flag, not as the
beginning of a field width.)
• An optional precision that gives the minimum number of digits to appear for the d, i, o, u,
x, and X conversions, the number of digits to appear after the decimalpoint character for a,
A, e, E, f, and F conversions, the maximum number of significant digits for the g and G
conversions, or the maximum number of characters to be written from a string in s
x, and X conversions, the number of digits to appear after the decimalpoint character for a,
A, e, E, f, and F conversions, the maximum number of significant digits for the g and G
conversions, or the maximum number of characters to be written from a string in s
conversions. The precision takes the form of a period (.) followed either by an asterisk * (described later) or by an optional decimal integer; if only the period is specified, the
precision is taken as zero. If a precision appears with any other conversion specifier, the behavior is undefined.
precision is taken as zero. If a precision appears with any other conversion specifier, the behavior is undefined.
•
An optional length modifier that specifies the size of the argument.
• A conversion specifier character that specifies the type of conversion to be applied.
As noted above, a field width, or precision, or both, may be indicated by an asterisk. In this case, an int argument supplies the field width or precision. The arguments specifying field width, or
precision, or both, shall appear (in that order) before the argument (if any) to be converted. A
negative field width argument is taken as a flag followed by a positive field width. A negative precision argument is taken as if the precision were omitted.
precision, or both, shall appear (in that order) before the argument (if any) to be converted. A
negative field width argument is taken as a flag followed by a positive field width. A negative precision argument is taken as if the precision were omitted.
The flag characters and their meanings are:
The result of the conversion is leftjustified within the field. (It is rightjustified if this flag is not specified.)
+
The result of a signed conversion always begins with a plus or minus sign. (It begins with a
sign only when a negative value is converted if this flag is not specified. The results of all
floating conversions of a negative zero, and of negative values that round to zero, include a
minus sign.)
sign only when a negative value is converted if this flag is not specified. The results of all
floating conversions of a negative zero, and of negative values that round to zero, include a
minus sign.)
space
If the first character of a signed conversion is not a sign, or if a signed conversion results in no
characters, a space is prefixed to the result. If the space and + flags both appear, the space flag
is ignored.
characters, a space is prefixed to the result. If the space and + flags both appear, the space flag
is ignored.
#
The result is converted to an "alternative form". For o conversion, it increases the precision, if
and only if necessary, to force the first digit of the result to be a zero (if the value and
precision are both 0, a single 0 is printed). For x (or X) conversion, a nonzero result has 0x
(or 0X) prefixed to it. For a, A, e, E, f, F, g, and G conversions, the result always contains a
decimalpoint character, even if no digits follow it. (Normally, a decimalpoint character
appears in the result of these conversions only if a digit follows it.) For g and G conversions,
trailing zeros are not removed from the result. For other conversions, the behavior is
undefined.
and only if necessary, to force the first digit of the result to be a zero (if the value and
precision are both 0, a single 0 is printed). For x (or X) conversion, a nonzero result has 0x
(or 0X) prefixed to it. For a, A, e, E, f, F, g, and G conversions, the result always contains a
decimalpoint character, even if no digits follow it. (Normally, a decimalpoint character
appears in the result of these conversions only if a digit follows it.) For g and G conversions,
trailing zeros are not removed from the result. For other conversions, the behavior is
undefined.
0
For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, leading zeros (following any
indication of sign or base) are used to pad to the field width; no space padding is performed.
If the 0 and flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversions, if
a precision is specified, the 0 flag is ignored. For other conversions, the behavior is
undefined.
If the 0 and flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversions, if
a precision is specified, the 0 flag is ignored. For other conversions, the behavior is
undefined.
The length modifiers and their meanings are:
hh
(C99) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a signed char or unsigned char argument (the argument will have been promoted according to the integer promotions, but its value shall be converted to signed char or unsigned
char before printing); or that a following n conversion specifier applies to a pointer to a
signed char argument.
char before printing); or that a following n conversion specifier applies to a pointer to a
signed char argument.
h
Specifies that a following d, i, o, u, x, or X conversion specifier applies to a short int or unsigned short int argument (the argument will have been promoted according to the integer promotions, but its value shall be converted to short int or unsigned short
int before printing); or that a following n conversion specifier applies to a pointer to a
short int argument.
int before printing); or that a following n conversion specifier applies to a pointer to a
short int argument.
l (ell)
Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long int or
unsigned long int argument; that a following n conversion specifier applies to a
pointer to a long int argument; (C99) that a following c conversion specifier applies to a
wint_t argument; (C99) that a following s conversion specifier applies to a pointer to a
wchar_t argument; or has no effect on a following a, A, e, E, f, F, g, or G conversion
specifier.
unsigned long int argument; that a following n conversion specifier applies to a
pointer to a long int argument; (C99) that a following c conversion specifier applies to a
wint_t argument; (C99) that a following s conversion specifier applies to a pointer to a
wchar_t argument; or has no effect on a following a, A, e, E, f, F, g, or G conversion
specifier.
ll (ellell)
(C99) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long long int or unsigned long long int argument; or that a following n conversion specifier applies to a pointer to a long long int argument.
j
(C99) Specifies that a following d, i, o, u, x, or X conversion specifier applies to an
intmax_t or uintmax_t argument; or that a following n conversion specifier applies to a pointer to an intmax_t argument.
z
(C99) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument; or that a following n conversion specifier
applies to a pointer to a signed integer type corresponding to size_t argument.
t
(C99) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a
ptrdiff_t or the corresponding unsigned integer type argument; or that a following n conversion specifier applies to a pointer to a ptrdiff_t argument.
L
Specifies that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument.
If a length modifier appears with any conversion specifier other than as specified above, the behavior is undefined.
The conversion specifiers and their meanings are:
d, i
d, i
The int argument is converted to signed decimal in the style [−]dddd. The precision
specifies the minimum number of digits to appear; if the value being converted can be
represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters.
o, u, x, X
The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal notation (x or X) in the style dddd; the letters abcdef are used for x
conversion and the letters ABCDEF for X conversion. The precision specifies the minimum
number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters.
conversion and the letters ABCDEF for X conversion. The precision specifies the minimum
number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters.
f, F
A double argument representing a (finite) floatingpoint number is converted to decimal
notation in the style [−]ddd.ddd, where the number of digits after the decimalpoint character is equal to the precision specification. If the precision is missing, it is taken as 6; if the
precision is zero and the # flag is not specified, no decimalpoint character appears. If a
decimalpoint character appears, at least one digit appears before it. The value is rounded to
the appropriate number of digits.
precision is zero and the # flag is not specified, no decimalpoint character appears. If a
decimalpoint character appears, at least one digit appears before it. The value is rounded to
the appropriate number of digits.
(C99) A double argument representing an infinity is converted in one of the styles []inf or []infinity — which style is implementationdefined. A double argument representing a NaN is converted in one of the styles []nan or []nan(ncharsequence) — which style, and the meaning of any ncharsequence, is implementationdefined. The F conversion
specifier produces INF, INFINITY, or NAN instead of inf, infinity, or nan,
respectively. (When applied to infinite and NaN values, the , +, and space flags have their usual meaning; the # and 0 flags have no effect.)
specifier produces INF, INFINITY, or NAN instead of inf, infinity, or nan,
respectively. (When applied to infinite and NaN values, the , +, and space flags have their usual meaning; the # and 0 flags have no effect.)
e, E
A double argument representing a (finite) floatingpoint number is converted in the style
[−]d.ddde±dd, where there is one digit (which is nonzero if the argument is nonzero) before the decimalpoint character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero and the # flag is not specified, no decimalpoint character appears. The value is rounded to the appropriate number of digits. The E conversion specifier produces a number with E instead of e introducing the exponent. The exponent always contains at least two digits, and only as many more digits as necessary to represent the exponent. If the value is zero, the exponent is zero.
(C99) A double argument representing an infinity or NaN is converted in the style of an f or F conversion specifier.
g, G
A double argument representing a (finite) floatingpoint number is converted in style f or e (or in style F or E in the case of a G conversion specifier), with the precision specifying the
number of significant digits. If the precision is zero, it is taken as 1. The style used depends on the value converted; style e (or E) is used only if the exponent resulting from such a
conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional portion of the result unless the # flag is specified; a decimalpoint
character appears only if it is followed by a digit.
number of significant digits. If the precision is zero, it is taken as 1. The style used depends on the value converted; style e (or E) is used only if the exponent resulting from such a
conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional portion of the result unless the # flag is specified; a decimalpoint
character appears only if it is followed by a digit.
(C99) A double argument representing an infinity or NaN is converted in the style of an f or F conversion specifier.
a, A
(C99) A double argument representing a (finite) floatingpoint number is converted in the
style [−]0xh.hhhhp±d, where there is one hexadecimal digit (which is nonzero if the
argument is a normalized floatingpoint number and is otherwise unspecified) before the
decimalpoint character (Binary implementations can choose the hexadecimal digit to the left
style [−]0xh.hhhhp±d, where there is one hexadecimal digit (which is nonzero if the
argument is a normalized floatingpoint number and is otherwise unspecified) before the
decimalpoint character (Binary implementations can choose the hexadecimal digit to the left
of the decimalpoint character so that subsequent digits align to nibble [4bit] boundaries.)
and the number of hexadecimal digits after it is equal to the precision; if the precision is
missing and FLT_RADIX is a power of 2, then the precision is sufficient for an exact
representation of the value; if the precision is missing and FLT_RADIX is not a power of 2,
then the precision is sufficient to distinguish (The precision p is sufficient to distinguish
and the number of hexadecimal digits after it is equal to the precision; if the precision is
missing and FLT_RADIX is a power of 2, then the precision is sufficient for an exact
representation of the value; if the precision is missing and FLT_RADIX is not a power of 2,
then the precision is sufficient to distinguish (The precision p is sufficient to distinguish
values of the source type if 16p-1 > bn where b is FLT_RADIX and n is the number of baseb
digits in the significand of the source type. A smaller p might suffice depending on the
implementation's scheme for determining the digit to the left of the decimalpoint character.)
values of type double, except that trailing zeros may be omitted; if the precision is zero and
the # flag is not specified, no decimalpoint character appears. The letters abcdef are used
for a conversion and the letters ABCDEF for A conversion. The A conversion specifier
produces a number with X and P instead of x and p. The exponent always contains at least one
digit, and only as many more digits as necessary to represent the decimal exponent of 2. If the
value is zero, the exponent is zero.
digits in the significand of the source type. A smaller p might suffice depending on the
implementation's scheme for determining the digit to the left of the decimalpoint character.)
values of type double, except that trailing zeros may be omitted; if the precision is zero and
the # flag is not specified, no decimalpoint character appears. The letters abcdef are used
for a conversion and the letters ABCDEF for A conversion. The A conversion specifier
produces a number with X and P instead of x and p. The exponent always contains at least one
digit, and only as many more digits as necessary to represent the decimal exponent of 2. If the
value is zero, the exponent is zero.
A double argument representing an infinity or NaN is converted in the style of an f or F conversion specifier.
c
If no l length modifier is present, the int argument is converted to an unsigned char, and the resulting character is written.
(C99) If an l length modifier is present, the wint_t argument is converted as if by an ls conversion specification with no precision and an argument that points to the initial element of a twoelement array of wchar_t, the first element containing the wint_t argument to the lc conversion specification and the second a null wide character.
s
If no l length modifier is present, the argument shall be a pointer to the initial element of an
array of character type. (No special provisions are made for multibyte characters.) Characters
from the array are written up to (but not including) the terminating null character. If the
precision is specified, no more than that many characters are written. If the precision is not
specified or is greater than the size of the array, the array shall contain a null character.
(C99) If an l length modifier is present, the argument shall be a pointer to the initial element
of an array of wchar_t type. Wide characters from the array are converted to multibyte
characters (each as if by a call to the wcrtomb function, with the conversion state described
by an mbstate_t object initialized to zero before the first wide character is converted) up to
and including a terminating null wide character. The resulting multibyte characters are written
up to (but not including) the terminating null character (byte). If no precision is specified, the
array shall contain a null wide character. If a precision is specified, no more than that many
characters (bytes) are written (including shift sequences, if any), and the array shall contain a
null wide character if, to equal the multibyte character sequence length given by the precision,
the function would need to access a wide character one past the end of the array. In no case is
a partial multibyte character written. (Redundant shift sequences may result if multibyte
characters have a statedependent encoding.)
array of character type. (No special provisions are made for multibyte characters.) Characters
from the array are written up to (but not including) the terminating null character. If the
precision is specified, no more than that many characters are written. If the precision is not
specified or is greater than the size of the array, the array shall contain a null character.
(C99) If an l length modifier is present, the argument shall be a pointer to the initial element
of an array of wchar_t type. Wide characters from the array are converted to multibyte
characters (each as if by a call to the wcrtomb function, with the conversion state described
by an mbstate_t object initialized to zero before the first wide character is converted) up to
and including a terminating null wide character. The resulting multibyte characters are written
up to (but not including) the terminating null character (byte). If no precision is specified, the
array shall contain a null wide character. If a precision is specified, no more than that many
characters (bytes) are written (including shift sequences, if any), and the array shall contain a
null wide character if, to equal the multibyte character sequence length given by the precision,
the function would need to access a wide character one past the end of the array. In no case is
a partial multibyte character written. (Redundant shift sequences may result if multibyte
characters have a statedependent encoding.)
p
The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printable characters, in an implementationdefined manner.
n
The argument shall be a pointer to signed integer into which is written the number of
characters written to the output stream so far by this call to fprintf. No argument is
converted, but one is consumed. If the conversion specification includes any flags, a field width, or a precision, the behavior is undefined.
%
A % character is written. No argument is converted. The complete conversion specification shall be %%.
If a conversion specification is invalid, the behavior is undefined. If any argument is not the correct type for the corresponding coversion specification, the behavior is undefined.
In no case does a nonexistent or small field width cause truncation of a field; if the result of a
conversion is wider than the field width, the field is expanded to contain the conversion result.
conversion is wider than the field width, the field is expanded to contain the conversion result.
For a and A conversions, if FLT_RADIX is a power of 2, the value is correctly rounded to a hexadecimal floating number with the given precision.
It is recommended practice that if FLT_RADIX is not a power of 2, the result should be one of the two adjacent numbers in hexadecimal floating style with the given precision, with the extra
stipulation that the error should have a correct sign for the current rounding direction.
stipulation that the error should have a correct sign for the current rounding direction.
It is recommended practice that for e, E, f, F, g, and G conversions, if the number of significant
decimal digits is at most DECIMAL_DIG, then the result should be correctly rounded. (For binary
todecimal conversion, the result format's values are the numbers representable with the given
format specifier. The number of significant digits is determined by the format specifier, and in the
case of fixedpoint conversion by the source value as well.) If the number of significant decimal
digits is more than DECIMAL_DIG but the source value is exactly representable with
DECIMAL_DIG digits, then the result should be an exact representation with trailing zeros.
Otherwise, the source value is bounded by two adjacent decimal strings L < U, both having
DECIMAL_DIG significant digits; the value of the resultant decimal string D should satisfy L ≤ D
≤ U , with the extra stipulation that the error should have a correct sign for the current rounding
direction.
todecimal conversion, the result format's values are the numbers representable with the given
format specifier. The number of significant digits is determined by the format specifier, and in the
case of fixedpoint conversion by the source value as well.) If the number of significant decimal
digits is more than DECIMAL_DIG but the source value is exactly representable with
DECIMAL_DIG digits, then the result should be an exact representation with trailing zeros.
Otherwise, the source value is bounded by two adjacent decimal strings L < U, both having
DECIMAL_DIG significant digits; the value of the resultant decimal string D should satisfy L ≤ D
≤ U , with the extra stipulation that the error should have a correct sign for the current rounding
direction.
The fprintf function returns the number of characters transmitted, or a negative value if an output or encoding error occurred.
The printf function is equivalent to fprintf with the argument stdout interposed before the arguments to printf. It returns the number of characters transmitted, or a negative value if an
output error occurred.
output error occurred.
The sprintf function is equivalent to fprintf, except that the argument s specifies an array into which the generated input is to be written, rather than to a stream. A null character is written at the end of the characters written; it is not counted as part of the returned sum. If copying takes place between objects that overlap, the behavior is undefined. The function returns the number of
characters written in the array, not counting the terminating null character.
characters written in the array, not counting the terminating null character.
The vfprintf function is equivalent to fprintf, with the variable argument list replaced by
arg, which shall have been initialized by the va_start macro (and possibly subsequent va_arg calls). The vfprintf function does not invoke the va_end macro. The function returns the
number of characters transmitted, or a negative value if an output error occurred.
number of characters transmitted, or a negative value if an output error occurred.
The vprintf function is equivalent to printf, with the variable argument list replaced by arg, which shall have been initialized by the va_start macro (and possibly subsequent va_arg calls). The vprintf function does not invoke the va_end macro. The function returns the
number of characters transmitted, or a negative value if an output error occurred.
number of characters transmitted, or a negative value if an output error occurred.
The vsprintf function is equivalent to sprintf, with the variable argument list replaced by
arg, which shall have been initialized by the va_start macro (and possibly subsequent va_arg
calls). The vsprintf function does not invoke the va_end macro. If copying takes place
between objects that overlap, the behavior is undefined. The function returns the number of
| Arrays →
calls). The vsprintf function does not invoke the va_end macro. If copying takes place
between objects that overlap, the behavior is undefined. The function returns the number of
| Arrays →
0 comments:
Post a Comment