Binghamton University. CS-220 Spring Includes & Streams

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

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

Standard File Pointers

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

Input / Output Functions

UNIX input and output

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

Standard C Library Functions

DATA STRUCTURES USING C

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

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

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

Advanced C Programming Topics

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

C Basics And Concepts Input And Output

Should you know scanf and printf?

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

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

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

File IO and command line input CSE 2451

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

CpSc 1111 Lab 4 Formatting and Flow Control

Library Functions. General Questions

EE458 - Embedded Systems Lecture 4 Embedded Devel.

C programming basics T3-1 -

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

Lecture 03 Bits, Bytes and Data Types

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

BİL200 TUTORIAL-EXERCISES Objective:

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

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

CS Programming In C

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

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

C PROGRAMMING. Characters and Strings File Processing Exercise

System Software Experiment 1 Lecture 7

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

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

Goals of this Lecture

Lecture 8: Structs & File I/O

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

PROGRAMMAZIONE I A.A. 2017/2018

Computational Methods of Scientific Programming Fall 2007

Introduction to Computing Lecture 03: Basic input / output operations

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

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

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

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

The Design of C: A Rational Reconstruction (cont.)

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

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

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

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)

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

Input/Output: Advanced Concepts

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

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

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

Fundamentals of Programming. Lecture 11: C Characters and Strings

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

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

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

3COM0271 Computer Network Protocols & Architectures A

Input/Output and the Operating Systems

Simple Output and Input. see chapter 7

ENG120. Misc. Topics

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

CS240: Programming in C

CSC209 Review. Yeah! We made it!

today cs3157-fall2002-sklar-lect05 1

Computer Programming Unit v

Standard I/O in C and C++

File I/O. Preprocessor Macros

CpSc 1111 Lab 5 Formatting and Flow Control

Fundamentals of Programming

Fundamental of Programming (C)

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

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

C Libraries. Bart Childs Complementary to the text(s)

CSI 402 Lecture 2 Working with Files (Text and Binary)

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

Console Input and Output

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

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

SWEN-250 Personal SE. Introduction to C

CS 261 Fall Mike Lam, Professor. Structs and I/O

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

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

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Data and File Structures Chapter 2. Basic File Processing Operations

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Introduction to C An overview of the programming language C, syntax, data types and input/output

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

C for C++ Programmers

Transcription:

Includes & Streams 1

C Pre-Processor C Program Pre-Processor Pre-processed C Program Header Files Header Files 2

#include Two flavors: #include <abc.h> Replace this line with the contents of file abc.h found in the system libraries #include xyz.h Replace this line with the contents of file xyz.h found in the current directory Concept: Write code once, use it in many different programs By convention, included files are called header files and have a.h file type 3

Header (.h) files May include any valid C code may include further #includes Typically small only what you need to invoke outside functions Function prototype declarations Sometimes, data and/or type declarations Typically, a header file describes a group of functions related functions e.g. functions which deal with Input and Output Typically associated with a.c file with the same name.c file contains function definitions more later 4

C Standard Library Part of C itself Definitions/Descriptions included in C standard Available with every C compiler Primarily, a set of C functions we all can use Keep C language simple Wrap complicated stuff in function definitions We don t need to know about the implementation of the complicated stuff Full description of standard library available as reference Programming in C, Appendix B 5

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 6

Unix standard streams stdout stdin Command stderr 7

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

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

Simple IO functions putc(y); stdout stdin x=getc(); stderr Read from stdin, write to stdout No opens/closes required 10

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 11

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 one or more characters before printing Format specifiers may consume one of the extra arguments Need an extra argument for each format specifier that requires a value 12

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

Format Specifier Modifiers Format Specifier: %<flag><width><.precision><type> <flag> : optional - (left 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 > 14

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

functions for streams fopen(filename,mode) returns data of type 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 Must flclose as well! #include <stdio.h> FILE mystr=fopen( example.txt, r ); if (mystr==null) { /* open failed! */ } while(2==fscanf(mystr, %s=%d,name,&val)) { // handle name and value } fclose(mystr); 16

Resources Programming in C, Appendix B, Chapter 15 Wikipedia: printf format string (https://en.wikipedia.org/wiki/printf_format_string) 17