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

Similar documents
C FILE Type. Basic I/O in C. Accessing a stream requires a pointer of type FILE.

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer variable of type FILE.

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

Standard File Pointers

File IO and command line input CSE 2451

Fundamentals of Programming

Fundamental of Programming (C)

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

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

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

System Software Experiment 1 Lecture 7

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

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

File I/O. Last updated 10/30/18

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Input/Output and the Operating Systems

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

HIGH LEVEL FILE PROCESSING

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

Should you know scanf and printf?

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

Console Input and Output

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Programming in C Quick Start! Biostatistics 615 Lecture 4

File I/O. Arash Rafiey. November 7, 2017

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

Arithmetic Expressions in C

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Unit 4. Input/Output 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

Operators and Control Flow. CS449 Fall 2017

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

Input / Output Functions

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

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

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

File Handling in C. EECS 2031 Fall October 27, 2014

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

DATA STRUCTURES USING C

Course organization. Course introduction ( Week 1)

25.2 Opening and Closing a File

Continued from previous lecture

Reserved Words and Identifiers

Lecture 9: File Processing. Quazi Rahman

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Binghamton University. CS-220 Spring Includes & Streams

Topic 6: A Quick Intro To C

C++ Input/Output: Streams

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Fundamentals of C. Structure of a C Program

Fundamentals of Programming. Lecture 11: C Characters and Strings

2. Numbers In, Numbers Out

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

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)

Standard I/O in C and C++

2. Numbers In, Numbers Out

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

Course Outline Introduction to C-Programming

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

The Design of C: A Rational Reconstruction: Part 2

Outline. Computer Programming. Preprocessing in C. File inclusion. Preprocessing in C

Computer Programming Unit v

Introduction to Computing Lecture 03: Basic input / output operations

Input/Output: Advanced Concepts

Input/output functions

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Standard C Library Functions

File Access. FILE * fopen(const char *name, const char * mode);

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CpSc 1111 Lab 4 Formatting and Flow Control

Fundamental of Programming (C)

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

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

Basic Types and Formatted I/O

Pointer Casts and Data Accesses

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Engineering program development 7. Edited by Péter Vass

Introduction to Programming

Summer June 15, 2010

(T) x. Casts. A cast converts the value held in variable x to type T

Introduction to C. Systems Programming Concepts

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

EL2310 Scientific Programming

Formatted Input/Output

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

Getting Started. Project 1

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

CS240: Programming in C

Library Functions. General Questions

// file2.c. // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2. int A2 = 12; // 2.1 int B2; // 2.2. extern int A2; // 1.

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

BİL200 TUTORIAL-EXERCISES Objective:

Simple Output and Input. see chapter 7

Transcription:

Stream Model of I/O 1 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. input file To be, or not to be? T o b e o r... That is the question. executing process 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. header file: <stdio.h>

C FILE Type Accessing a stream requires a pointer of type FILE. 2 C provides three standard streams, which require no special preparation other than the necessary include directive: stdin standard input keyboard stdout standard output console window stderr standard error console window Alternatively, you can declare FILE pointers and manage their connections to disk files.

Output with printf() 3 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.

Output Examples The specification of format is mildly complex: 4 int i = 42, j = 17; double x = 3.1415962; printf("i =%3d j =%3d\n", i, j); printf("x =%7.4f\n", x); i = 42 j = 17 x = 3.1416 There is an excellent discussion of the details in section 22.3 of the King book.

More on printf() Format Specifiers 5 The general form of a printf() specifier is: flags min width % #0 12.5 precision L length modifier conversion specifier g flags: optional, more than one allowed - left-justify within field + always show leading sign space precede non-negative numbers with a space # see reference 0 pad with zeros to fill field

More on printf() Format Specifiers 6 The general form of a printf() specifier is: flags min width % #0 12.5 precision L length modifier conversion specifier g min width: optional, pad with spaces if field not filled, ignored if insufficient precision: optional, number of digits for integer values, number of digits after decimal point for float/double

More on printf() Format Specifiers 7 The general form of a printf() specifier is: flags min width % #0 12.5 precision L length modifier conversion specifier g length modifier: optional, indicates value has a type that's longer or shorter than is normal for a particular conversion specifier (see following) d u hd ld normally used for int values normally used for unsigned int values normally used for short int values normally used for long int values see reference for more Table 22.5

More on printf() Format Specifiers 8 The general form of a printf() specifier is: flags min width % #0 12.5 precision L length modifier conversion specifier g 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

Format Specifiers for <stdint.h> 9 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 <inttypes.h> provides specialized format codes for the new integer types. Here's a very brief description; see King 27.2 for details. PRIdN for signed integer types, N = 8, 16, 32, 64 PRIuN for unsigned integer types For example: uint64_t K = 123456789012345; printf("%15"priu64"\n", K); // note use of quotes!!

Input with scanf() 10 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.

Input Examples 11 Suppose we have an input stream of the following form: 17 42 3.14159625 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); 17 42 3.1416 Suppose we have an input stream of the following form: 17, 42, 3.14159625 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); 17 42 3.1416

More on scanf() Format Specifiers 12 The general form of a scanf() specifier is: % max width length modifier * 12 L g conversion specifier *: optional, read but do not assign value to an object max width: optional, leading whitespace doesn't count

More on scanf() Format Specifiers 13 The general form of a scanf() specifier is: % max width length modifier * 12 L g 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 22.11 conversion specifier: mandatory see reference for more Table 22.12

Opening Files 14 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 mode path/name of file to be opened "r" "w" "a" "rb" "wb" "ab" "r+" "w+" "a+" see reference for more Return value is valid FILE pointer if successful and NULL otherwise.

File I/O 15 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*.

Closing Files 16 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.