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

Standard File Pointers

Input / Output Functions

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

File IO and command line input CSE 2451

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

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

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

25.2 Opening and Closing a File

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

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

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

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

Goals of this Lecture

I/O Management! Goals of this Lecture!

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

I/O Management! Goals of this Lecture!

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

Advanced C Programming Topics

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

C programming basics T3-1 -

Standard C Library Functions

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

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

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

DATA STRUCTURES USING C

UNIX input and output

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)

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

System Software Experiment 1 Lecture 7

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Input/Output and the Operating Systems

Lecture 03 Bits, Bytes and Data Types

C Basics And Concepts Input And Output

Should you know scanf and printf?

Organization of a file

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Lecture 7: file I/O, more Unix

EE458 - Embedded Systems Lecture 4 Embedded Devel.

Programming & Data Structure

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

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

2009 S2 COMP File Operations

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

Computational Methods of Scientific Programming Fall 2007

Process Management! Goals of this Lecture!

PROGRAMMAZIONE I A.A. 2017/2018

ENG120. Misc. Topics

File Handling. 21 July 2009 Programming and Data Structure 1

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

C PROGRAMMING. Characters and Strings File Processing Exercise

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

Simple Output and Input. see chapter 7

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

Input/Output: Advanced Concepts

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

Today s Learning Objectives

CSC 1107: Structured Programming

CSC 1107: Structured Programming

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

Pointers and File Handling

SWEN-250 Personal SE. Introduction to C

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

File I/O Lesson Outline

3COM0271 Computer Network Protocols & Architectures A

Library Functions. General Questions

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

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

CpSc 1111 Lab 4 Formatting and Flow Control

Standard I/O in C and C++

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

CSE 333 SECTION 3. POSIX I/O Functions

Lecture 8: Structs & File I/O

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

structs as arguments

Basic and Practice in Programming Lab 10

CS240: Programming in C

Text Output and Input; Redirection

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

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Lecture 23: System-Level I/O

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

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

Computer Programming Unit v

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

211: Computer Architecture Summer 2016

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

8. Characters, Strings and Files

Fundamentals of Programming

CSE 12 Spring 2016 Week One, Lecture Two

CSC 270 Survey of Programming Languages. Input and Output

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Stream Model of I/O. Basic I/O in C

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Fundamental of Programming (C)

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.

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

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

Two Output Streams? Original concept: stdout is an output stream for data designed to be consumed by programs stderr is an output stream for error messages designed to be consumed by humans However students learn to write to stdout first, and prefer stdout for both kinds of data As a result, most messages are written to stdout

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

Example: stripblanks #include <stdio.h> int main(int argc, char **argv) { char x; while(eof!=(x=getchar())) { if (x!= ' ') putchar(x); } return 0; } >./stripblanks This is a test Thisisatest _

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 7

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 8

Format Specifier Types Specifier Meaning %% Replace with a single % %c Replace with a single character argument %i or %d Replace with an integer argument converted to ASCII %x Replace with an integer argument converted to hexadecimal ASCII %f Replace with a floating point argument converted to ASCII %s Replace with a string argument (null terminated 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 9

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

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 in memory and update memory 11

Stream Names Each stream has a name The three standard streams are named : stdin, stdout, and stderr Declared in stdio.h Opened for you when you start your program 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 Standard I/O Function Default Stream Named Stream Equivalent getchar() stdin getc(stdin) putchar(x) stdout putc(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 fclose 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); 14

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 17

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 19

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) 22