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

Similar documents
Standard I/O in C, Computer System and programming in C

File (1A) Young Won Lim 11/25/16

25.2 Opening and Closing a File

Input/Output and the Operating Systems

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

C PROGRAMMING Lecture 6. 1st semester

UNIX System Programming

Jacobs University Bremen February 7 th, 2017 Dr. Kinga Lipskoch. Practice Sheet. First Name:... Last Name:... Matriculation Number:...

File I/O. Preprocessor Macros

File Handling. Reference:

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

System Software Experiment 1 Lecture 7

Operating System Labs. Yuanbin Wu

File and Console I/O. CS449 Spring 2016

System Programming. Standard Input/Output Library (Cont d)

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

C-Refresher: Session 10 Disk IO

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

CP2 Revision. theme: file access and unix programs

File IO and command line input CSE 2451

COMP 2355 Introduction to Systems Programming

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

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

CS240: Programming in C

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

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

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

2009 S2 COMP File Operations

File and Console I/O. CS449 Fall 2017

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

Standard File Pointers

Computer Programming Unit v

CSE 410: Systems Programming

CSE 303 Lecture 15. C File Input/Output (I/O) reading: Programming in C Ch. 16; Appendix B pp

PROGRAMMAZIONE I A.A. 2017/2018

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

CSC 1107: Structured Programming

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

CSC 1107: Structured Programming

EM108 Software Development for Engineers

Computer programming

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

CS240: Programming in C

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.

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

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

Input / Output Functions

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

Intermediate Programming, Spring 2017*

a = ^ ^ ^ ^ ^ ^ ^ b = c = a^b =

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

Data File and File Handling

UNIT- 2. Binary File

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

Goals of this Lecture

Standard C Library Functions

I/O Management! Goals of this Lecture!

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

I/O Management! Goals of this Lecture!

Operating System Labs. Yuanbin Wu

Course organization. Course introduction ( Week 1)

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

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

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

Summer June 15, 2010

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

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

HIGH LEVEL FILE PROCESSING

Princeton University Computer Science 217: Introduction to Programming Systems. Modularity design principles

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

C Basics And Concepts Input And Output

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

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

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)

structs as arguments

EXAMINATION REGULATIONS and REFERENCE MATERIAL for ENCM 339 Fall 2017 Section 01 Final Examination

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

Getting Started. Project 1

SAE1A Programming in C. Unit : I - V

csc/cpe 357 C Quiz Archive

Student Number: Instructor: Reid Section: L0201 (12:10-1:00pm)

Advanced C Programming Topics

Pointers, Arrays, and Strings. CS449 Spring 2016

Princeton University Computer Science 217: Introduction to Programming Systems. Modularity design principles

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

CSC 270 Survey of Programming Languages. Input and Output

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

C PROGRAMMING. Characters and Strings File Processing Exercise

C mini reference. 5 Binary numbers 12

INTRODUCTION TO UNIX FILE SYSTEM:

Week 9 Lecture 3. Binary Files. Week 9

Text Output and Input; Redirection

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Simple Output and Input. see chapter 7

Transcription:

Chapter 5, Standard I/O Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based #include <stdio.h> FILE *f; Standard files (FILE *varname) variable: stdin File Number: STDIN_FILENO variable: stdout File Number: STDOUT_FILENO variable: stderr File Number: STDERR_FILENO

Buffering in Stdio Buffers used by stdio 3 methods of buffering available Fully (block) buffered Line buffered printf ("Hello..."); printf ("Hello...\n"); Unbuffered int setvbuf(file *stream, char *buf, int mode, size_t size); setbuf(3), setbuffer(3), setlinebuf(3) also. int fflush(file *stream); write stream s buffer to OS int fpurge(file *stream); erase any input or output in the buffer for the stream

Standard Operations: FILE *fopen (char *path, char *mode) mode: "r" -- read, must exist "w" -- write, may create "a" -- append, may create "r+" -- read & write, must exist "w+" -- write & read, may create "a+" -- append & read, may create "rb", "wb", "ab", "rb+",... -- binary version returns pointer to a stream, NULL => error. FILE *fdopen (int fd, char *mode) int fclose(file *stream)

Input via Stdio int fgetc(file *stream); (function) int getc(file *stream); (macro) int getchar(); (macro) int ungetc(int c, FILE *stream); char * gets(char *str); (UNSAFE!) char * fgets(char * restrict str, int size, FILE * restrict stream); int feof(file *stream); int ferror(file *stream); void clearerr(file *stream); int fileno(file *stream);

Output via Stdio int fputc(int c, FILE *stream); (function) int putc(int c, FILE *stream); (macro) int putchar(int c); (macro) int fputs(const char * restrict str, FILE * restrict stream); int puts(const char *str); /* adds \n */

Binary I/O Not what one would expect. size_t fread(void * ptr, size_t size, size_t nmemb, FILE * stream); size_t fwrite(const void * ptr, size_t size, size_t nmemb, FILE * stream); Usage: Array based! float d[10]; if (fwrite (d, sizeof(float), 10, fp)!= 10 )... int a[35]; if (fread (a, sizeof(int), 35, fp)!= 35 )...

Positioning a stream int fseek(file *stream, long int offset, int whence); long int ftell(file *stream); void rewind(file *stream); int fgetpos(file * restrict stream, fpos_t * restrict pos); int fsetpos(file * restrict stream, const fpos_t * restrict pos);

Formatted Output int printf(const char * format,...); int fprintf(file * stream, const char * format,...); int sprintf(char * str, const char * format,...); int snprintf(char * str, size_t size, const char * format,...); Formats: %[flags][fieldwidth][.precision][modifier]conv %d %i (%ld %sd) -- Integer (long, short) %x -- hex output of an integer %o -- octal output of an integer %c -- Char %s -- String %e, %f,... -- Real numbers %% -- print % (No conversion)

Formatted Output (page 2) Formats: %[flags][fieldwidth][.precision][modifier]conv Flags - Left Justify + Always sign the number 0 zero padded (space) blank space for + field width number That many columns * uses an integer from parameters for number precision (floating, strings) number number of digits after decimal point number of characters of a string * uses an integer from parameters for number

Examples #include <stdio.h> int main() { int ix; for (ix = 0; ix < 20; ix++) printf ("%*c%.*s\n", 39-ix,, ix*2+1, "*************************************************"); return 0; } #include <stdio.h> int main() { int i; printf ("%12s%14s\n", "num", "square"); for (i = 0; i<10; i++) printf ("%12d%14d\n", i, i*i); return 0; }

Formatted Input int scanf(const char * format,...); int fscanf(file * stream, const char * format,...); int sscanf(const char * str, const char * format,...); Returns number of input items assigned. Format: spaces: Match white space Other chars: Match that char Conversions: %[flag][field-width][modifier]conv * suppresses assignment % matches % s matches non-white-space characters stops at space or field-width chars [ minimal re matching -- to a string n number of characters consumed stored in int

Temporary files & names FILE * tmpfile(void); /usr/tmp/aaaa00738 char * tmpnam(char *str); char * tempnam(const char *tmpdir, const char *prefix);