Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O   4. Standard I/O Library 5. Files and Directories 6. System Data.

Slides:



Advertisements
Similar presentations
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Advertisements

Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
Text File Input and Output. Overview Text File Buffered I/O Functions fopen Function Demo of File Write and Read.
Folk/Zoellick/Riccardi, File Structures 1 Objectives: To get familiar with Logical files vs. physical files File processing operations File processing.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 System Calls & Stdio. 2 Two processes open the same file Both keep writing to it What happens?
Kernel File Interface operating systems (or programming I/O in Unix)
Structures and Unions Chapter 6. Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element.
File I/O.
NCHU System & Network Lab Lab 13 File I/O & Standard I/O.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Characters and Strings File Processing Exercise C Programming:Part 3.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
File IO and command line input CSE 2451 Rong Shi.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 7 Files By C. Shing ITEC Dept Radford University.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C Programming Lecture 12 : File Processing
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
Adv. UNIX:fp/101 Advanced UNIX v Objectives of these slides: –a more detailed look at file processing in C Special Topics in Comp. Eng.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Advanced Programming in the UNIX Environment Hop Lee.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
CSCE Systems Programming Lecture 07 – Make, Stdio, I/O Buffering CSCE 510 Jan 23, 2013.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
CHAPTER 5. STANDARD I/O LIBRARY System Programming 本份投影片大量參考熊博安教授的系統程式投影片 羅習五 國立中正大學資訊工程學系 EA-001 (05)
By C. Shing ITEC Dept Radford University
Lecture 12 CIS 208 Friday, March 3rd , 2005.
Chapter 4 File Processing
Chapter 9 – File Input Output
ECE Application Programming
Lecture 11 File input/output
TMF1414 Introduction to Programming
File I/O.
No Objects, No Type Safety
CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
What you need for the 1st phase of project
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Text and Binary File Processing
File Input and Output.
Henning Schulzrinne Columbia University
Module 12 Input and Output
Standard I/O Library Implementation
File I/O & UNIX System Interface
Professor Jodi Neely-Ritz University of Florida
I/O CS580U - Fall 2018.
Presentation transcript:

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_FILENO, STDOUT_FILENO, …

Buffering  Goal Use the minimum number of read and write calls.  Types  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 The filling of a fixed buffer could trigger I/O. The flushing of all line-buffered outputs if input is requested.  Unbuffered 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: #include int fflush(FILE *fp); All output streams are flushed if fp == NULL

Buffering #include 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 ( ) Optional size st_blksize (stat()) #define BUFSIZ 1024 ( ) 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 BUFSIZFB/LB NULLNB setvbuf FB non-null any size FB 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 #include 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 rwar+w+a+ File exists? Y Y Truncate Y Y R Y Y Y Y W Y Y Y Y Y W only at end YY

Standard I/O Library - Open #include 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 #include 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 #include 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 #include 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 #include 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 #include 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 #include 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 Function UsrCPU SysCPU Clock Prog Fig s 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/Fig s 397.9s Loops in char/line- at-a-time cycles! text space the same # of kernel calls!

Binary I/O Objectives Read/write a structure at a time, which could contains null or ‘ \n ’. #include 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 { short count; long total; char name[NAMESIZE]; } item; 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 #include 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 #include 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 #include 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 #include int scanf(const char *format, … ); int fscanf(FILE *fp, const char *format, … ); int sscanf(char *buf, const char *format, … );

Implementation Details #include int fileno(FILE *fp); Get filedes for fcntl, dup, etc See 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 #include char *tmpnam(char *ptr); TMP_MAX in /* = 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 #include char *tempnam(const char *directory, const char *prefix); TMPDIR *directory is null? P_tmpdir in /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