Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Similar documents
Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Binghamton University. CS-220 Spring Includes & Streams

Input / Output Functions

Standard File Pointers

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

File IO and command line input CSE 2451

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

25.2 Opening and Closing a File

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Advanced C Programming Topics

Should you know scanf and printf?

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

EE458 - Embedded Systems Lecture 4 Embedded Devel.

UNIX input and output

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

UNIT IV-2. The I/O library functions can be classified into two broad categories:

Standard C Library Functions

Contents. A Review of C language. Visual C Visual C++ 6.0

Lecture 03 Bits, Bytes and Data Types

Computer Programming: Skills & Concepts (CP) Files in C

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

System Software Experiment 1 Lecture 7

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

DATA STRUCTURES USING C

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

C programming basics T3-1 -

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

Input/Output and the Operating Systems

CSE 230 Intermediate Programming in C and C++ Input/Output and Operating System

Lecture 7: file I/O, more Unix

Computational Methods of Scientific Programming Fall 2007

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Standard I/O in C and C++

Process Management! Goals of this Lecture!

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

2009 S2 COMP File Operations

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

C Basics And Concepts Input And Output

Programming & Data Structure

Unit 4. Input/Output Functions

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

C PROGRAMMING. Characters and Strings File Processing Exercise

Simple Output and Input. see chapter 7

3COM0271 Computer Network Protocols & Architectures A

Organization of a file

ENG120. Misc. Topics

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

CSC 1107: Structured Programming

File Handling. 21 July 2009 Programming and Data Structure 1

CSC 1107: Structured Programming

CpSc 1111 Lab 4 Formatting and Flow Control

SWEN-250 Personal SE. Introduction to C

Introduction to Computing Lecture 03: Basic input / output operations

Today s Learning Objectives

PROGRAMMAZIONE I A.A. 2017/2018

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

Library Functions. General Questions

CSC 270 Survey of Programming Languages. Input and Output

Lecture 23: System-Level I/O

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Input/Output: Advanced Concepts

CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

CS 220: Introduction to Parallel Computing. Input/Output II. Lecture 8

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

CSE 12 Spring 2016 Week One, Lecture Two

Lecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting

Pointers and File Handling

AMCAT Automata Coding Sample Questions And Answers

Basic and Practice in Programming Lab 10

Text Output and Input; Redirection

Work relative to other classes

CS240: Programming in C

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

Friday, February 10, Lab Notes

Console Input and Output

Fundamentals of Programming

UNIX I/O. Computer Systems: A Programmer's Perspective, Randal E. Bryant and David R. O'Hallaron Prentice Hall, 3 rd edition, 2016, Chapter 10

Lecture 8: Structs & File I/O

Fundamental of Programming (C)

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

structs as arguments

File I/O Lesson Outline

Transcription:

Input and Output Streams and Stream IO 1

Standard Input and Output Need: #include <stdio.h> Large list of input and output functions to: Read and write from a stream Open a file and make a stream Close a file and remove the stream Create, Rename, delete, or move files Streams. directional list of data (bytes) input streams provide data to program output streams program provides data 2

Unix standard streams :./command stdout stdin Command stderr 3

Simple IO functions putchar(y); stdout stdin x=getchar(); stderr Read a single character from stdin, write to stdout No opens/closes required 4

Shared Monitor stdout and stderr both go to the monitor by default If a program writes to both stderr and stdout, sometimes the output gets scrambled >./timestable 2 0 1 ---- ---- ---- 0 0 DBG: About to write the first row, second column 0 1 0 1 ---- ---- ----

Redirection UNIX provides a way to change the allocation of standard streams When you execute a command, UNIX looks for command line suffixes: <filename tells UNIX to read filename file as stdin >filename tells UNIX to write filename file as stdout 2>filename tells UNIX to write filename file as stderr Suffixes come after command name and arguments: cat x.txt >y.txt Cannot connect same file to stdin and stdout Output files are created if not already there Output redirected files are re-written from the beginning Replaces whatever used to be in that file

Redirection:./command <in.txt >out.txt stdout out.txt in.txt stdin Command stderr 7

Pipes ( ) Connects two UNIX commands Connects the stdout of the first command to the stdin of the second Example: cat chapter*.txt wc w cat chapter*.txt writes chapter1.txt, chapter2.txt, to stdout wc w counts the number of words from stdin If first command stalls, second command waits If either the first or second command ends abnormally, you get a broken pipe message Can combine pipes and redirection: cat chap*.txt wc w >sum.txt

Pipes:./command1./command2 stdin Command1 stdout stdin Command2 stdout stderr stderr 9

Example: stripblanks #include <stdio.h> int main(int argc, char **argv) { char x; while(eof!=(x=getchar())) { if (x!= ' ') putchar(x); } return 0; } >cat xmp.txt This is an example. It has blanks >./stripblanks <xmp.txt Thisisanexample. Ithasblanks

Stream Names Each stream has a name The three standard streams are named : stdin, stdout, and stderr These are opened for you when you start your program These are closed for you when your program ends You can create your own named streams You must open the stream before using it You should close the stream when you are done with it Named streams have a direction (input or output streams)

Using Named Streams Almost all i/o functions have sister functions that use named streams I/O Function Default Stream Sister Function Equivalent getchar() stdin getch(stdin) putchar(x) stdout putch(stdout,x) printf( ) stdout fprintf(stdout, ) scanf( ) stdin fscanf(stdin, )

Example Named Stream fopen(filename,mode) returns FILE* which can be used as a stream filename is fully qualified, or relative to the current directory mode: r =read, w =write, a =write append flclose files that you open #include <stdio.h> FILE *myin=fopen( example.txt, r ); if (myin==null) { /* open failed! */ } char x; while( -1!= ( x=getc(myin) ) ) { /* Work on x */ } fclose(myin); 13

Print Formatted (printf) Function with prototype: int printf(char * str, ); str : format string (list of characters) : variable number of extra arguments printf formats the format string, using the extra arguments as input into a resulting formatted string Then, writes the formatted string to stdout Returns number of characters printed 14

Printf Formatting Every % in formatted string starts a special format specifier Format specifiers end with a format specifier type (next slide) Format specifiers are replaced with zero or more characters before printing Format specifiers usually consume one of the extra arguments Need an extra argument for each format specifier that requires a value 15

Format Specifier Types Specifier Meaning %% Replace with a single % %c Replace with next single character argument %i or %d Replace with next integer argument converted to ASCII %x Replace with next integer argument converted to hexadecimal ASCII %f Replace with next floating point argument converted to ASCII %s Replace with next string argument (list of characters) %p Replace with address in hexadecimal printf( Format c=%c, x=%d (or %x), f=%f s=%s\n, z,12,12,3.14, that s all folks ); Format c=z, x=12 (or 0C), f=3.1400 s=that s all folks 16

Format Specifier Modifiers Format Specifier: %flag width.precision type flag : optional - (left justified, default is right justified) width : optional minimum size of replacement.precision : optional number of decimal places for %f before rounding printf( Format <%5i> <%-5.2f> <%3c> <%-3c>\n, 10,3.156, a, b ); Format < 10> <3.16 > < a> <b > 17

format string functions printf write output to stdout fprintf write output to a named stream (stdout, stderr, etc.) sprintf write output to string scanf read from stdin and update memory that arguments point to fscanf read from specified stream, and update memory sscanf read from a string and update memory 18

Stream Buffers In order to improve program efficiency, streams are often buffered A stream buffer is a block of memory that accumulates stream data until there is enough data to be efficiently read or written Stream buffers mean that the program interacts with the buffer (fast memory) many times before having to interact with real (slow) IO Sometimes, (especially when debugging) buffers are a problem Debug message gets stuck in buffer and not printed when program dies Remove buffer by setting its size to zero: setbuf(stdout,0);

Stream Buffer Example for(i=0; i<totsize; i++) { putchar(out[i]); }

Resources The C Programming Language, Chapter 7 Wikipedia: printf format string (https://en.wikipedia.org/wiki/printf_format_string) C Standard Library Reference stdio.h (https://wwws.acm.illinois.edu/webmonkeys/book/c_guide/2.12.html) C FAQ Stdio (http://c-faq.com/stdio/index.html) 21