1 / 11

Lecture 13

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 .

dionne
Télécharger la présentation

Lecture 13

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 13 Input/Output Files

  2. 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.

  3. printf() Conversion Characters Conversion character How the corresponding argument is printed c as a character d,i as a decimal integer u as an unsigned decimal integer o as an unsigned octal integer x,X as an unsigned hexadecimal integer e as a floating-point number: 7.123000e+00 E as a floating-point number: 7.123000E+00 g in the shorter of the e-format or f-format G in the shorter of the E-format or f-format s as a string p the corresponding argument is a pointer to void; it prints as a hexadecimal number. n 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.

  4. 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.

  5. 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.

  6. 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);

  7. 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.

  8. 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”);

  9. File Modes Modes for opening files “r” open text file for reading “w” open text file for writing “r+” open text file (read and write) “a” open text file for appending “rb” open binary file for reading “wb” open binary file for writing “ab” open binary file for appending

  10. 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.

  11. 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.

More Related