Basic I/O in C Computer Organization I 1 August 2009 ©2006-09 McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.

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

CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
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.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Programming in C #2 Input / Output.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
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.
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.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 4 Input and Output.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
Chapter 18 I/O in C.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
File IO and command line input CSE 2451 Rong Shi.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
CSE1301 Computer Programming: Lecture 6 Input/Output.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
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.
C is a high level language (HLL)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
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.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Formatted Input and Output
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
C FILE Type Accessing a stream requires a pointer variable of type FILE. C provides three standard streams, which require no special preparation other.
TMF1414 Introduction to Programming
Chapter 18 I/O in C.
File I/O.
Introduction to C CSE 2031 Fall /3/ :33 AM.
CSC215 Lecture Input and Output.
File Input/Output.
Programming in C Input / Output.
Input and Output Lecture 4.
Chapter 08- printf and scanf
Programming in C Input / Output.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Lecture 13 Input/Output Files.
Chapter 18 I/O in C.
File Input and Output.
Chapter 18 I/O in C.
Programming in C Input / Output.
Module 12 Input and Output
C FILE Type The original version of these notes was based somewhat on Chapter 1 in C++ for Java Programmers by Weiss. Accessing a stream requires a pointer.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Professor Jodi Neely-Ritz University of Florida
Chapter 18 I/O in C.
Presentation transcript:

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 between the process that initializes it and an object, such as a file, which may be viewed as a sequence of data. In the simplest view, a stream object is simply a serialized view of that other object. To be, or not to be? That is the question. input file executing process o T be o. r.. We think of data as flowing in the stream to the process, which can remove data from the stream as desired. The data in the stream cannot be lost by “flowing past” before the program has a chance to remove it.

Basic I/O in C Computer Organization I 2 August 2009 © McQuain, Feng & Ribbens C FILE Type Accessing a stream requires a pointer of type FILE. C provides three standard streams, which require no special preparation other than the necessary include directive: stdin standard inputkeyboard stdout standard outputconsole window stderr standard errorconsole window Alternatively, you can declare FILE pointers and manage their connections to disk files.

Basic I/O in C Computer Organization I 3 August 2009 © McQuain, Feng & Ribbens Output with printf() The Standard Library provides the printf() function which can be used to write formatted output to the standard output stream, stdout. int printf(const char * restrict format,...); The first parameter is a string literal that specifies the formatting to be used. The remaining parameters, if any, specify the values to be written. The return value is the number of characters that were written, or a negative value if an error was encountered.

Basic I/O in C Computer Organization I 4 August 2009 © McQuain, Feng & Ribbens Output Examples The specification of format is mildly complex: int i = 42, j = 17; double x = ; printf("i =%3d j =%3d\n", i, j); printf("x =%7.4f\n", x); There is an excellent discussion of the details in section 22.3 of the King book. i = 42 j = 17 x =

Basic I/O in C Computer Organization I 5 August 2009 © McQuain, Feng & Ribbens More on printf() Format Specifiers The general form of a printf() specifier is: flags:optional, more than one allowed - left-justify within field + always show leading sign spaceprecede non-negative numbers with a space # see reference 0 pad with zeros to fill field %#012.5Lg flags min width precision length modifier conversion specifier

Basic I/O in C Computer Organization I 6 August 2009 © McQuain, Feng & Ribbens More on printf() Format Specifiers The general form of a printf() specifier is: min width:optional, pad with spaces if field not filled, ignored if insufficient %#012.5Lg flags min width precision length modifier conversion specifier precision:optional, number of digits for integer values, number of digits after decimal point for float/double

Basic I/O in C Computer Organization I 7 August 2009 © McQuain, Feng & Ribbens More on printf() Format Specifiers The general form of a printf() specifier is: length modifier:optional, indicates value has a type that's longer or shorter than is normal for a particular conversion specifier (see following) d normally used for int values u normally used for unsigned int values hd normally used for short int values ld normally used for long int values see reference for more… Table 22.5 %#012.5Lg flags min width precision length modifier conversion specifier

Basic I/O in C Computer Organization I 8 August 2009 © McQuain, Feng & Ribbens More on printf() Format Specifiers The general form of a printf() specifier is: conversion specifier:mandatory d, i converts int to decimal form f, F converts double to decimal form; default precison 6 c displays int as unsigned character see reference for more… Table 22.6 %#012.5Lg flags min width precision length modifier conversion specifier

Basic I/O in C Computer Organization I 9 August 2009 © McQuain, Feng & Ribbens Format Specifiers for The basic integer format codes will work with int32_t and uint32_t (with compiler warnings), but are not reliable with int64_t and uint64_t. The header file provides specialized format codes for the new integer types. Here's a very brief description; see King 27.2 for details. PRIdNfor signed integer types, N = 8, 16, 32, 64 PRIuNfor unsigned integer types For example: uint64_t K = ; printf("%15"PRIu64"\n", K); // note use of quotes!!

Basic I/O in C Computer Organization I 10 August 2009 © McQuain, Feng & Ribbens Input with scanf() The Standard Library provides the scanf() function which can be used to write formatted output to the standard output stream, stdin. int scanf(const char * restrict format,...); The first parameter is a string literal that specifies the formatting expected in the input stream. The remaining parameters, if any, specify the variables that will receive the values that are read. The return value is the number of values that were read, or the value of EOF if an input failure occurs.

Basic I/O in C Computer Organization I 11 August 2009 © McQuain, Feng & Ribbens Input Examples Suppose we have an input stream of the following form: int i = 1, j = 1; double x = 1.5; scanf("%d %d %f", &i, &j, &x); printf("%5d %5d %7.4f\n", i, j, x); int i = 1, j = 1; double x = 1.5; scanf("%d, %d, %f", &i, &j, &x); printf("%5d %5d %7.4f\n", i, j, x); Suppose we have an input stream of the following form: 17, 42,

Basic I/O in C Computer Organization I 12 August 2009 © McQuain, Feng & Ribbens More on scanf() Format Specifiers The general form of a scanf() specifier is: %*12Lg max width length modifier conversion specifier max width:optional, leading whitespace doesn't count * :optional, read but do not assign value to an object

Basic I/O in C Computer Organization I 13 August 2009 © McQuain, Feng & Ribbens More on scanf() Format Specifiers The general form of a scanf() specifier is: %*12Lg max width length modifier conversion specifier length modifier:optional, indicates object that will receive value has a type that's longer or shorter than is normal for a particular conversion specifier (see following) see reference for more… Table conversion specifier:mandatory see reference for more… Table 22.12

Basic I/O in C Computer Organization I 14 August 2009 © McQuain, Feng & Ribbens Opening Files File I/O is almost identical to I/O with stdin and stdout. You must make a call in order to associate a FILE pointer with a particular file: FILE *fopen(const char* restrict filename, const char* restrict mode); filename path/name of file to be opened mode"r" "w" "a" "rb" "wb" "ab" "r+" "w+" "a+" see reference for more Return value is valid FILE pointer if successful and NULL otherwise.

Basic I/O in C Computer Organization I 15 August 2009 © McQuain, Feng & Ribbens File I/O File I/O is accomplished using variations of printf() and scanf() : int fprintf(FILE * restrict stream, const char * restrict format,...); int fscanf(FILE * restrict stream, const char * restrict format,...); These are used in the same way as their counterparts, aside from taking a FILE*.

Basic I/O in C Computer Organization I 16 August 2009 © McQuain, Feng & Ribbens Closing Files When done with a file, you should close the file: int fclose(FILE *stream); Any unwritten buffered data will be delivered to the host environment. Any unread buffered data will be discarded. Returns 0 on success and EOF otherwise.