fprintf()
Writes to a file.
Synopsis
int fprintf(file file, string format, [,<type1> var1 [,<type2>
var2...]]);
Parameters
Parameter | Description |
---|---|
file | File to be written. Use the constants stdin, stdout, stderr (see Reference Tables). |
format | output format |
var1, var2, ... | arguments |
Return value
In the event of errors, the function returns -1 or EOF otherwise, the number of written characters
Error
If incorrect data types are used or if a file is not open.
Description
Writes outputs to the file "file"; under the specified format. The format string format contains two types of elements. Usual characters that are copied to the output and (optional) conversion specifications that define the conversion and output of the next-following (optional) argument var1, var2 and so on of fprintf(). The function fprintf() returns the number of written characters or in the event of an error, a negative number. Each conversion specification starts with the character % and ends with a conversion character. The following flags can be placed in between:
Flags | No or several flags in any order. |
- | The converted argument is aligned left-justified. If no minus sign has been specified, the alignment will be right-justified. |
space | Similar to the flag + (output with positive sign if the value has been signed). A blank space is outputted instead of a plus sign. If the flags space and + are specified together, the flag + has the higher priority. |
# | Output is in an alternative format. A detailed description of the individual conversion characters follows in the next section. |
-
width: The indication of the accuracy is optional. The exact meaning depends on the type of the conversion performed.
-
prec: Indication of the accuracy is optional. The accuracy indicates the number of digits that are output after the decimal point. The accuracy is entered after the decimal point. If the decimal point is used without specifying the accuracy, no places after the decimal point will be output. The specification of the accuracy only applies to the conversionse,E, f,gandG.
-
format: A letter that defines the type of conversion.
Description | Meaning of width | Meaning of the flag # | |
---|---|---|---|
d | The argument is represented as a decimal number |
Minimum number of represented characters. Otherwise filled with blank spaces. The default value is 1. The output of the number zero with a width of zero results in no character! |
Not defined. |
o | The argument is represented as an octal number without leading zeros. | seed | Places a 0 in front of the result is greater than zero. |
u | The argument is displayed as an unsigned decimal number. | seed | Not defined. |
x | The argument is represented as a hexadecimal number (without leading 0x). The letters abcdef are used. | seed | Places a 0 in front of the result is greater than zero. |
X | Like x. But the letters ABCDEF are used. | see d | Places an 0 in front of the result is greater than zero. |
f |
The argument float/double is represented as a decimal number. [ - ] ddd.ddd is the default: |
Minimum number of represented characters. May be extended for the required accuracy by specifying a decimal point and places after the decimal point. When a decimal point is output, at least one character is output before the decimal point. |
Output of a decimal point even if no spaces after the decimal point follow. |
e |
The argument float/double is represented as an exponent number in the format [ - ] d.ddde dd The exponent always contains at least two places. If the value is zero, the exponent is zero. |
see f | see f |
E | Similar to e. Instead of e,E is used. | see f | see f |
g | Similar to f or e, but dependent on the value. The conversion type e is only used when the exponent is less than -4 or greater than the accuracy. | see f | see f |
G | Similar to g. An E is output instead of an e. | see f | see f |
s | The argument is a string. Individual characters are output from the string until either a null character ("/0") has been reached or as many characters as required for accuracy. | Minimum number of represented characters. | Not defined. |
Example
The following example writes three values to a temporary file and then immediately reads them out again.
main()
{
file f;
int i;
dyn_float value;
value[1]=12.123;
value[2]=8.0;
value[3]=3.1;
f=fopen("C:/TEMP/tfile.TXT","w+");
//opens a new file for writing and reading
for (i=1;i<=3;i++) fprintf(f,"%5.3f\n",value[i]);
//write "float" values
rewind(f); // Back to the beginning
for (i=1;i<=3;i++)
{
fscanf (f,"%f",value[i]); // read as float again
DebugN(value[i]); // output for checking
}
fclose(f);
}
Assignment
File functions
Availability
CTRL