Lecture13.ppt

advertisement
Lecture 13
Input/Output
Files
printf()
printf(control string, other arguments);

The expressions in other_arguments are
evaluated and converted according to the
formats in the control string and are then
placed in the output stream.
printf(“%-14sPayRate: $%-4.2f\n”, “James Smith”, 8.95);
James Smith

Pay Rate: $8.95
Characters in the control string that are not
part of a format are placed directly in the
output stream.
printf() Conversion Characters
Conversion
character
c
d,i
u
o
x,X
e
E
g
G
s
p
n
%
How the corresponding argument is printed
as a character
as a decimal integer
as an unsigned decimal integer
as an unsigned octal integer
as an unsigned hexadecimal integer
as a floating-point number: 7.123000e+00
as a floating-point number: 7.123000E+00
in the shorter of the e-format or f-format
in the shorter of the E-format or f-format
as a string
the corresponding argument is a pointer to
void; it prints as a hexadecimal number.
argument is a pointer to an integer into
which the number of characters written so far
is printed; the argument is not converted.
with the format %% a single % is written; there
is no corresponding argument to be converted.
printf( ) Conversion Specifications

field width (optional)
• An optional positive integer
• If the converted argument has fewer characters
than the specified width, it will be padded
with spaces on the left or right depending on
the left or right justification.
• If the converted argument has more characters,
the field width will be extended to whatever is
required.

precision (optional)
• Specified by a period followed by a nonnegative
integer.
• Minimum number of digits to be printed for d,
i, o, u, x, and X conversions.
• Minimum number of digits to the right of the
decimal point for e, E, and f conversions.
• Maximum number of significant digits for G and
g conversions.
• Maximum number of characters to be printed for
an s conversion.
Flag Characters in
Conversion Specifications

Minus sign • Left justified in field.

Plus sign +
• + prepended to a non-negative number

Space
• space prepended to a nonnegative number

#
• Result is converted to an alternate form
that depends on the conversion character.
– In an x or X conversion, the # causes 0x or 0X
to be prepended to the hexadecimal number.

Zero 0
• Zeros instead of spaces are used to pad the
field.
Using Variables in
in Conversion Specifications

The field width, precision, or both may
be specified by an asterisk * instead
of an integer.
• Indicates that a value is to be obtained
from the argument list.
int
m, n;
double x = 333.7777777;
. . . /* get m and n from somewhere */
printf(“x = %*.*f\n”, m, n, x);
Properties of Files




For our purposes, a file is a
collection of related information that
is stored on a magnetic disk or CD-ROM
drive.
Files have a name.
Files must be opened and closed.
Files can be:
• written to
• read from
• appended to

When we open a file, we must tell which
of the above three activities we will
be performing.
fopen( )
fopen() opens a named file in a
particular mode and returns a file
pointer.
fopen(file_name, mode);
 Example

FILE * ifp;
char file_name[] = “data.in”;
…
ifp = fopen(file_name, “r”);
or
ifp = fopen(“data.in”, “r”);
File Modes
Modes for opening files
“r”
open text file for reading
“w”
“r+”
“a”
“rb”
“wb”
“ab”
open
open
open
open
open
open
text file for writing
text file (read and write)
text file for appending
binary file for reading
binary file for writing
binary file for appending
Considerations in Opening a File



Trying to open for reading a file that
cannot be read, or does not exist:
• fopen() returns a NULL pointer.
Opening a file for writing:
• Causes the file to be created if it
does not exist and overwritten if it
does exist.
Opening a file in append mode:
• Causes the file to be created if it
does not exist and causes writing to
occur at the end of the file if it
does exist.
Direct Input/Output (see p. 581)
 The
functions fread() and
fwrite() are used to read and
write binary files.
• No conversions are performed as
they are with fscanf().
• In certain applications (those
that use structures), the use of
these functions can save
considerable time.
– Because one fread or fwrite can
input or output an entire
structure.
Download