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

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

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

UNIX System Programming

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

Input/Output and the Operating Systems

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

25.2 Opening and Closing a File

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

C PROGRAMMING Lecture 6. 1st semester

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

File I/O. Preprocessor Macros

CS240: Programming in C

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

C-Refresher: Session 10 Disk IO

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

File Handling. Reference:

Operating System Labs. Yuanbin Wu

HIGH LEVEL FILE PROCESSING

File and Console I/O. CS449 Spring 2016

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

PROGRAMMAZIONE I A.A. 2017/2018

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

System Software Experiment 1 Lecture 7

File IO and command line input CSE 2451

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

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

File and Console I/O. CS449 Fall 2017

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

Computer programming

CSE 410: Systems Programming

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

CP2 Revision. theme: file access and unix programs

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Data File and File Handling

Computer Programming Unit v

Input / Output Functions

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

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

Standard C Library Functions

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

File System User API

EM108 Software Development for Engineers

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

2009 S2 COMP File Operations

Operating System Labs. Yuanbin Wu

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

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

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

CS240: Programming in C

Naked C Lecture 6. File Operations and System Calls

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

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

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

Standard File Pointers

Text Output and Input; Redirection

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

C Basics And Concepts Input And Output

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

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

UNIT- 2. Binary File

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

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)

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

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

C PROGRAMMING. Characters and Strings File Processing Exercise

Course organization. Course introduction ( Week 1)

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

ENG120. Misc. Topics

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

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

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

Goals of this Lecture

Simple Output and Input. see chapter 7

#define PERLIO_NOT_STDIO 0 /* For co-existence with stdio only */ #include <perlio.h> /* Usually via #include <perl.h> */

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

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

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

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

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

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

Summer June 15, 2010

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

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

Exercise Session 3 Systems Programming and Computer Architecture

INTRODUCTION TO UNIX FILE SYSTEM:

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

Introduc)on to I/O. CSE 421/521: Opera)ng Systems Karthik Dantu

CSI 402 Lecture 2 (More on Files) 2 1 / 20

M.CS201 Programming language

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Introduction to file management

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

Intermediate Programming, Spring 2017*

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

Main Program Revisited. Reading Assignment. K.N. King Chapter 3, 22. K.N. King Sections Example of Argument Processing

The float and long double versions were an invention of C89, where they were optional; however, in C99, they are required.

Transcription:

Standard I/O in C,

Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library 5. Files and Directories 6. System Data Files and Information 7. Environment of a Unix Process 8. Process Control 9. Signals 10.Inter-process Communication

Standard I/O Library Defined by Dennis Ritchie in 1975 An ANSI C standard Easy to use and portable Details handled: Buffer allocation, optimal-sized I/O chunks, better interface, etc.

Standard I/O Library Difference from File I/O File Pointers vs File Descriptors fopen vs open When a file is opened/created, a stream is associated with the file. FILE object File descriptor, buffer size, # of remaining chars, an error flag, and the like. stdin, sdtout, stderr defined in <stdio.h> STDIO_FILENO, STDOUT_FILENO,

Buffering Goal Types Use the minimum number of read and write calls. Fully Buffered Actual I/O occurs when the buffer is filled up. A buffer is automatically allocated when the first-time I/O is performed on a stream. flush: standard I/O lib vs terminal driver

Buffering Line Buffered Perform I/O when a newline char is encountered! usually for terminals. Caveats Unbuffered The filling of a fixed buffer could trigger I/O. The flushing of all line-buffered outputs if input is requested. Expect to output asap, e.g. using write() E.g., stderr

Buffering ANSI C Requirements Fully buffered for stdin and stdout. Standard error is never fully buffered. Functions: int fflush(file *fp); All output streams are flushed if fp == NULL

Buffering void setbuf(file *fp, char *buf); void setvbuf(file *fp, char *buf, int mode, size_t size); Full/line buffering if buf is not NULL (BUFSIZ) Terminals mode: _IOFBF, IOLBF, _IONBF (<stdio.h>) Optional size st_blksize (stat()) #define BUFSIZ 1024 (<stdio.h>) They must be called before any operation is performed on the streams!

Buffering Possible Memory Access Errors Use automatic allocation NULL for *buf in setvbuf() bookkeeping mode buf len type setbuf non-null BUFSIZ FB/LB NULL setvbuf FB non-null any size FB NB FB NULL st_blksize FB LB non-null any size LB LB NULL st_blksize LB NB ignored no buffered NB

Standard I/O Library - Open FILE *fopen(const char *pathname, const char *type); FILE *freopen(const char *pathname, const char *type, FILE fp); fopen/freopen opens a specified file! POSIX.1 Close fp stream first! New files created by a or w have r/w rights for all Type r w a r+ w+ a+ File exists? Y Y Truncate Y Y R Y Y Y Y W Y Y Y Y Y W only at end Y Y

Standard I/O Library - Open FILE *fdopen(int fildes, const char *type); Associate a standard I/O stream with an existing file descriptor POSIX.1 Pipes, network channels No truncating for the file for w b (in rb, wb, ab, r+b, ) stands for a binary file no effect for Unix kernel O_APPEND supports multiple access. Interleaved R&W restrictions intervening fflush (WR), fseek(wr/rw), fsetpos (WR/RW), rewind (WR/RW), EOF (RW)

Standard I/O Library Open/Close int fclose(file *fp); Flush buffered output Discard buffered input All I/O streams are closed after the process exits. setbuf or setvbuf to change the buffering of a file before any operation on the stream.

Standard I/O Library Reading/Writing Unformatted I/O Character-at-a-time I/O, e.g., getc Buffering handled by standard I/O lib Line-at-a-time I/O, e.g., fgets Buffer limit might need to be specified. Direct I/O, e.g., fread Read/write a number of objects of a specified size. An ANSI C term, e.g., = object-at-a-time I/O

Standard I/O Library Reading/Writing int getc(file *fp); int fgetc(file *fp); int getchar(void); getchar == getc(stdin) Differences between getc and fgetc getc could be a macro Argument s side effect, exec time, passing of the function address. unsigned char converted to int in returning

Standard I/O Library Reading/Writing int ferror(file *fp); int feof(file *fp); void clearerr(file *fp); int ungetc(int c, FILE *fp); An error flag and an EOF flag for each FILE No pushing back of EOF (i.e., -1) No need to be the same char read!

Standard I/O Library Reading/Writing int putc(int c, FILE *fp); int fputc(int c, FILE *fp); int putchar(int c); putchar(c) == putc(c, stdout) Differences between putc and fputc putc() can be a macro.

Line-at-a-Time I/O char *fgets(char *buf, int n, FILE *fp); Include \n and be terminated by null Could return a partial line if the line is too long. char *gets(char *buf); Read from stdin. No buffer size is specified -> overflow *buf does not include \n and is terminated by null.

Line-at-a-Time I/O char *fputs(const char *str, FILE *fp); Include \n and be terminated by null. No need for line-at-a-time output. char *puts(const char *str); *str does not include \n and is terminated by null. puts then writes \n to stdout.

Standard I/O Efficiency Program 5.1 Page 131 Copy stdin to stdout: getc putc Program 5.2 Page 132 Copy stdin to stdout: fgets fputs Loops in char/lineat-a-time cycles! Function UsrCPU SysCPU Clock Prog Fig3.1 0.0s 0.3s 0.3s fgets, fputs 2.2s 0.3s 2.6s 184B getc, putc 4.3s 0.3s 4.8s 384B fgetc, fputc 4.6s 0.3s 5.0s 152B 1B/Fig3.1 23.8s 397.9s 423.4 the same # of kernel calls! text space

Binary I/O Objectives Read/write a structure at a time, which could contains null or \n. size_t fread(void *ptr, size_t size, size_t nobj, FILE *fp); size_t fwrite(const void *ptr, size_t size, size_t nobj, FILE *fp); Reads less than the specified number of objects error or EOF ferror, feof Write error if less than the specified number of objects are written.

Binary I/O Example 1 float data[10]; if (fwrite(&data[2], sizeof(float), 4, fp)!= 4) err_sys( fwrite error ); Example 2 struct { } item; short count; long total; char name[namesize]; if (fwrite(&item, sizeof(item), 1, fp)!= 1) err_sys( fwrite error );

Binary I/O Not portable for programs using fread and fwrite 1. The offset of a member in a structure can differ between compilers and systems (due to alignment). 2. The binary formats for various data types, such as integers, could be different over different machines.

Positioning-a-Stream long ftell(file *fp); int fseek(file *fp, long offset, int whence); void rewind(file *fp); Assumption: a file s position can be stored in a long (since Version 7) whence: same as lseek Binary files: No requirements for SEEK_END under ANSI C (good under Unix, possible padding for other systems). Text files: SEEK_SET only 0 or returned value by ftell (different formats for some sys).

Positioning-a-Stream long fgetpos(file *fp, fpos_t *pos); int fsetpos(file *fp, const fpos_t *pos); ANSI C standard Good for non-unix systems A new data type fpos_t

Formatted I/O Output int printf(const char *format, ); int fprintf(file *fp, const char *format, ); int sprintf(char *buf, const char *format, ); Overflow is possible for sprintf() \0 appended at the end of the string. int vprintf(const char *format, var_list arg); int vfprintf(file *fp, const char *format, var_list arg); int vsprintf(char *buf, const char *format, var_list arg);

Formatted I/O Input int scanf(const char *format, ); int fscanf(file *fp, const char *format, ); int sscanf(char *buf, const char *format, );

Implementation Details int fileno(file *fp); Get filedes for fcntl, dup, etc See <stdio.h> for per-stream flags, etc. Program 5.3 Page 139 Printing buffering for various I/O streams stdin, stdout line-buffered, buf size stderr unbuffered, buf size files: fully-buffered, buf size

Temporary Files char *tmpnam(char *ptr); TMP_MAX in <stdio.h> /* = 25, ANSI C */ If ptr == null, the pointer to the pathname is returned (L_tmpnam # of bytes assumed if ptr!= null). FILE *tmpfile(void); wb+ an empty binary file. Unlink the file immediately after it is created! Program 5.4 Page 141 tmpnam and tmpfile

Temporary Files char *tempnam(const char *directory, const char *prefix); TMPDIR *directory is null? P_tmpdir in <stdio.h> /tmp /* prefix could be up to 5 chars */ Not POSIX.1 and ANSI C, but XPG3 (SVR4, 4.3+BSD) Program 5.5 Page 142 tempnam

Alternatives to Standard I/O Main Issue Too many data copyings kernel standard I/O buffer standard I/O buffer our buffer Alternatives Fast I/O Library (fio) pointer sfio Represent files/memory regions under I/O streams, and stack processing modules above I/O streams. mmap