Lecture 13 Input/Output Files.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
File IO and command line input CSE 2451 Rong Shi.
Dale Roberts Basic I/O (Chap. 9) CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Chapter 3 Input and Output
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
5-1 Embedded Systems C Programming Language Review and Dissection III Lecture 5.
CSE1301 Computer Programming: Lecture 6 Input/Output.
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP C Part III.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 1704 Introduction to Data Structures and Software Engineering.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Input & Output Operations Week 6 SCP1103 Basic C Programming SEM1 2010/2011.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
ECE Application Programming
Formatted Input and Output
Chapter 9 C Formatted Input/Output
ECE Application Programming
Input/output.
TMF1414 Introduction to Programming
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
No Objects, No Type Safety
Introduction to C CSE 2031 Fall /3/ :33 AM.
File Input/Output.
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 08- printf and scanf
Programming in C Input / Output.
Formatting Output.
Chapter 9 - Formatted Input/Output
Department of Computer and Information Science
Programming in C Input / Output.
Conversion Check your class notes and given examples at class.
Introduction to C EECS May 2019.
EECE.2160 ECE Application Programming
File I/O.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

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

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

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.