lecture 13

11
Lecture 13 Lecture 13 Input/Output Input/Output Files Files

Upload: dionne

Post on 05-Jan-2016

27 views

Category:

Documents


0 download

DESCRIPTION

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 . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 13

Lecture 13Lecture 13

Input/OutputInput/OutputFilesFiles

Page 2: Lecture 13

printf()printf()

printfprintf((control stringcontrol string, , other argumentsother arguments););

The expressions in The expressions in other_argumentsother_arguments are are evaluated and converted according to the evaluated and converted according to the formatsformats in the in the control stringcontrol string and are then and are then placed in the placed in the output streamoutput stream..

printf(“%-14sprintf(“%-14sPayRate:PayRate: $$%-4.2f\n”, “James Smith”, 8.95);%-4.2f\n”, “James Smith”, 8.95);

James Smith James Smith Pay Rate: $Pay Rate: $8.958.95

Characters in the control string that are Characters in the control string that are not not part of a formatpart of a format are placed are placed directlydirectly in the in the output stream.output stream.

Page 3: Lecture 13

printf() Conversion printf() Conversion CharactersCharacters

ConversionConversioncharactercharacter How the corresponding argument is printedHow the corresponding argument is printedcc as a characteras a characterd,id,i as a decimal integeras a decimal integeruu as an unsigned decimal integeras an unsigned decimal integeroo as an unsigned octal integeras an unsigned octal integerx,Xx,X as an unsigned hexadecimal integeras an unsigned hexadecimal integeree as a floating-point number: 7.123000as a floating-point number: 7.123000ee+00+00EE as a floating-point number: 7.123000as a floating-point number: 7.123000EE+00+00gg in the shorter of the in the shorter of the ee-format or f-format-format or f-formatGG in the shorter of the in the shorter of the EE-format or f-format-format or f-formatss as a stringas a stringpp the corresponding argument is a pointer tothe corresponding argument is a pointer to

void; it prints as a hexadecimal number.void; it prints as a hexadecimal number.nn argument is a pointer to an integer intoargument is a pointer to an integer into

which the number of characters written so farwhich the number of characters written so faris printed; the argument is not converted.is printed; the argument is not converted.

%% with the format with the format %%%% a single % is written; a single % is written; therethere

is no corresponding argument to be converted.is no corresponding argument to be converted.

Page 4: Lecture 13

printf( ) Conversion printf( ) Conversion SpecificationsSpecifications

field width (optional)field width (optional)• An optional positive integerAn optional positive integer• If the converted argument has If the converted argument has fewer charactersfewer characters than the specified width, it will be padded than the specified width, it will be padded with spaces on the left or right depending on with spaces on the left or right depending on the left or right justification.the left or right justification.

• If the converted argument has If the converted argument has more charactersmore characters, , the field width will be the field width will be extendedextended to whatever is to whatever is required.required.

precision (optional)precision (optional)• Specified by a period followed by a nonnegative Specified by a period followed by a nonnegative integer.integer.

• MinMinimum number of digits to be printed for imum number of digits to be printed for dd, , ii, , oo, , uu, , xx, and , and XX conversions. conversions.

• MinMinimum number of digits to the right of the imum number of digits to the right of the decimal point for decimal point for ee, , EE, and , and ff conversions. conversions.

• MaxMaximum number of significant digits for imum number of significant digits for GG and and gg conversions. conversions.

• MaxMaximum number of characters to be printed for imum number of characters to be printed for an an ss conversion. conversion.

Page 5: Lecture 13

Flag Characters in Flag Characters in Conversion SpecificationsConversion Specifications

Minus sign Minus sign --• Left justified in field.Left justified in field.

Plus sign Plus sign ++• ++ prepended to a non-negative number prepended to a non-negative number

SpaceSpace• space prepended to a nonnegative numberspace prepended to a nonnegative number

##• Result is converted to an alternate form Result is converted to an alternate form that depends on the conversion character.that depends on the conversion character.– In an x or X conversion, the # causes 0x or 0X In an x or X conversion, the # causes 0x or 0X to be prepended to the hexadecimal number.to be prepended to the hexadecimal number.

Zero Zero 00• Zeros instead of spaces are used to pad the Zeros instead of spaces are used to pad the field.field.

Page 6: Lecture 13

Using Variables in Using Variables in in Conversion Specificationsin Conversion Specifications

The field width, precision, or both may The field width, precision, or both may be specified by an asterisk * instead be specified by an asterisk * instead of an integer.of an integer.• Indicates that a value is to be obtained Indicates that a value is to be obtained from the argument list.from the argument list.int m, n;int m, n;doubledouble x = 333.7777777; x = 333.7777777;

. . . . . . /* get m and n from somewhere *//* get m and n from somewhere */

printf(“x = %*.*f\n”, m, n, x);printf(“x = %*.*f\n”, m, n, x);

Page 7: Lecture 13

Properties of FilesProperties of Files

For our purposes, a file is a For our purposes, a file is a collection of related information that collection of related information that is stored on a magnetic disk or CD-ROM is stored on a magnetic disk or CD-ROM drive.drive.

Files have a Files have a namename.. Files must be Files must be openedopened and and closedclosed.. Files can be:Files can be:

• written towritten to• read fromread from• appended toappended to

When we When we openopen a file, we a file, we must tellmust tell which which of the above three activities we will of the above three activities we will be performing. be performing.

Page 8: Lecture 13

fopen( )fopen( )

fopen()fopen() opens a opens a namednamed file in a file in a particular particular modemode and returns a and returns a file pointerfile pointer..fopenfopen((file_namefile_name, , modemode););

ExampleExampleFILE * ifp;FILE * ifp;char file_name[] = “data.in”;char file_name[] = “data.in”;……ifp = fopen(file_name, “r”);ifp = fopen(file_name, “r”);ororifp = fopen(ifp = fopen(““data.indata.in””, “r”);, “r”);

Page 9: Lecture 13

File ModesFile Modes

Modes for opening filesModes for opening files““r”r” open text file for readingopen text file for reading

““w”w” open text file for writingopen text file for writing

““r+”r+” open text file (read and write)open text file (read and write)

““a”a” open text file for appendingopen text file for appending

““rb”rb” open binary file for readingopen binary file for reading

““wb”wb” open binary file for writingopen binary file for writing

““ab”ab” open binary file for appendingopen binary file for appending

Page 10: Lecture 13

Considerations in Opening Considerations in Opening a Filea File

Trying to open for Trying to open for readingreading a file that a file that cannot be read, cannot be read, oror does not exist: does not exist:• fopen() returns a fopen() returns a NULLNULL pointer. pointer.

Opening a file for Opening a file for writingwriting::• Causes the file to be Causes the file to be createdcreated if it if it does not exist and does not exist and overwrittenoverwritten if it if it does exist.does exist.

Opening a file in Opening a file in appendappend mode: mode:• Causes the file to be Causes the file to be createdcreated if it if it does not exist and causes writing to does not exist and causes writing to occur at the occur at the endend of the file if it of the file if it does exist.does exist.

Page 11: Lecture 13

Direct Input/Output (see p. Direct Input/Output (see p. 581)581)

The functions The functions fread()fread() and and fwrite()fwrite() are used to read and are used to read and write write binarybinary files. files.• No conversions are performed as No conversions are performed as they are with fscanf().they are with fscanf().

• In certain applications (those In certain applications (those that use structures), the use of that use structures), the use of these functions can save these functions can save considerable time.considerable time.– Because one fread or fwrite can Because one fread or fwrite can input or output an input or output an entire entire structurestructure..