Standard Input and Output. Overview Data communication with a C program and the outside world is performed through files Files are a non-volatile way.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

UNIT 15 File Processing.
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
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.
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.
Guide To UNIX Using Linux Third Edition
Programming in C #2 Input / Output.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
 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.
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.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
RjpSystem Level Programming Operating Systems 1 Having fun withy the Unix Operating System Praxis Week 7 Rob Pooley.
Lecture 13. Outline Standard Input and Output Standard Input and Output (I/O)– Review & more Buffered/unbuffered input Character I/O Formatted I/O Redirecting.
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.
Chapter 18 I/O in C.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
File IO and command line input CSE 2451 Rong Shi.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
CSE1301 Computer Programming: Lecture 6 Input/Output.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
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.
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.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets! Alphanumeric.
C Formatted Input/Output
Chapter 7 Text Input/Output Objectives
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
INTRODUCTION Every language has some features that provides interaction between the program and the user of the program. C language uses the reference.
Chapter 7 Text Input/Output Objectives
TMF1414 Introduction to Programming
Chapter 18 I/O in C.
Getting Started with C.
Plan for the Day: I/O (beyond scanf and printf)
Programming in C Input / Output.
Programming in C Input / Output.
CSI 121 Structured Programming Language Lecture 7: Input/Output
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Chapter Four UNIX File Processing.
File Input and Output.
Programming in C Input / Output.
Weeks 9-10 IO System Calls Standard IO (stdin, stdout) and Pipes
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
I/O CS580U - Fall 2018.
Presentation transcript:

Standard Input and Output

Overview Data communication with a C program and the outside world is performed through files Files are a non-volatile way to store data by means of such media as tape, CD-ROM, floppy disk, ZIP disk, hard drive, etc. C (just like the UNIX operating system) considers all process communication media to be files –An ordinary file on a disk is considered to be a file –So is the keyboard, the screen, parallel ports, and serial ports

Communication through Files Programs access files through basic file operations –Open a file –Read data from a file –Write data to a file –Close a file Text files store all data types as character bytes; the way a program reads the data (either text or binary mode) determines how the data is interpreted –Example: 12 z rti room –character?, character string?, integer?, floating point number?

stdin, stdout, stderr Three files are automatically opened each time a C program is run and automatically closed when a C program ends: stdin, stdout, and stderr stdin –Standard input, default source is the keyboard stdout –Standard output, default destination is the screen stderr –Standard error, default destination is the screen These three variable names are referred to as file descriptors; each is a handle (or pointer) to a record (i.e., struct) describing the actual file

C Program Entrances and Exits stdout (Screen) stdin (Keyboard) stderr (Screen) Constants and variables Operations and functions main XYZ ABCD Instructions and operating procedures

Files as Pipes for Data Program to compute average stdinstdout Average is Files act as pipes to bring a stream of data into the program and send a stream of data out of the program The program reads data from stdin and writes data to stdout as if they were ordinary files

One Byte At a Time Data is read or written one byte at a time from a file until the end of the file is reached or until the file is closed. The file system uses a pointer to keep track of the next byte to read or to write Smith Jack Manager 15 Hanson Susan Operator 7 Jones Nancy Administrator 10 Doe Carl Technician 12 In this file, the program has read the data as far as the letter 'J'. The next character to be read is 'a'

Functions Using stdin and stdout-1 Some standard C functions implicitly use stdin and stdout –scanf("%d", &aNumber); –aSymbol = getchar(); –gets(theBuffer); // High security risk –printf("Average: %5.2f", theAverage); –putchar(aCharacter); –puts(aPhrase);

Functions Using stdin and stdout-2 Other functions need to have stdin and stdout specified as the file descriptor –fscanf(stdin, "%d", &aNumber); –aSymbol = fgetc(stdin); –fgets(theBuffer, sizeof(theBuffer), stdin); –fprintf(stdout, "Average: %5.2f", theAverage); –fputc(aCharacter, stdout); –puts(aPhrase, stdout);

Input and Output Redirection Because a C program considers stdin and stdout to be ordinary files, a user can redirect the input and output to another source or destination That source or destination can be an I/O device or an ordinary file This redirection can be done when a program is run from the DOS or UNIX command line The '<' sign redirects input from a file The '>' sign redirects output to a new file The ">>" sign redirects output and appends the data to a current file or creates the file if it doesn't yet exist

Example of I/O Redirection Input comes from a file, output goes to the screen –C:\myprogram <numbers.dat Input comes from the keyboard, output goes to a file –C:\myprogram >results.txt Input comes from a file; output goes to a file –C:\myprogram findings.dat Input comes from the keyboard and the output is appended to the contents in a file –C:\myprogram >>findings.dat

Some Redirection to Try Out in MS-DOS Example #1 –C:\dir >dirwords.txt –C:\type dirwords.txt Example #2 –C:\dir >dir.dat –C:\find ".c" <dir.dat –C:\find ".c" findings.txt –C:\find ".exe" >findings.txt –C:\type findings.txt

printf Function The printf function writes formatted output to stdout –Ex. printf("Average: %5.2f \n", theAverage); The first argument is a character string containing ordinary text, escape characters, and format specifiers Syntax for format specifier: %. Each escape character is a combination of a '\' and a character, which represents a nonviewable ASCII character. An example is \n for newline The order, number, and type of the format specifiers corresponds to the order, number, and type of the arguments in the printf call

printf Examples No format specifiers, one escape character, no other arguments –printf("Name Age Address Distance\n"); One format specifier, two arguments –printf("Results is %d\n", theResult); Four format specifiers, five arguments –printf("%c %d %f %s", aCharacter, anInteger, aFloat, aString); Same as above, but with field width and precision specified –printf("%4c %3d %5.3f %8s", aCharacter, anInteger, aFloat, aString);

scanf Function The scanf function reads formatted input from stdin –scanf("%d", &aNumber); The first argument is a character string containing one or more conversion specifiers Syntax for format specifier: % The order, number, and type of the conversion specifiers corresponds to the order, number, and type of the other arguments in the scanf call Each argument value must by the address of the variable, NOT the variable itself, where the value should be stored To designate the address for a character, integer, structure, or floating point variable, precede the variable name by the & operator, which means "address of“ Array names are a special case when passed as an argument

scanf Examples One format specifier, two arguments –scanf("%d", &theResult); Three format specifiers, four arguments –scanf("%d%f%s", &anInteger, &aFloat, aString); Same as above, but in a different order –scanf("%s %f %d", aString, &aFloat, &anInteger);