CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

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

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

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

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

C Programming Language

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Computer System and programming in C

Computer Programming Unit v

System Software Experiment 1 Lecture 7

25.2 Opening and Closing a File

HIGH LEVEL FILE PROCESSING

Intermediate Programming, Spring 2017*

EM108 Software Development for Engineers

C Programming 1. File Access. Goutam Biswas. Lect 29

Input / Output Functions

Input/Output and the Operating Systems

CS240: Programming in C

C-Refresher: Session 10 Disk IO

Data File and File Handling

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

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

Computer programming

PROGRAMMAZIONE I A.A. 2017/2018

File Handling. Reference:

UNIT- 2. Binary File

Lecture 9: File Processing. Quazi Rahman

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

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

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

[6 marks] All parts of this question assume the following C statement. Parts (b) through (e) assume a variable called ptrs.

Week 9 Lecture 3. Binary Files. Week 9

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

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Standard C Library Functions

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

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

2009 S2 COMP File Operations

ENG120. Misc. Topics

Standard File Pointers

File IO and command line input CSE 2451

we are here Page 1 Recall: How do we Hide I/O Latency? I/O & Storage Layers Recall: C Low level I/O

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

UNIX System Programming

Goals of this Lecture

we are here I/O & Storage Layers Recall: C Low level I/O Recall: C Low Level Operations CS162 Operating Systems and Systems Programming Lecture 18

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

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

Engineering program development 7. Edited by Péter Vass

File I/O. Preprocessor Macros

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

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Chapter 11 File Processing

CSE 333 SECTION 3. POSIX I/O Functions

C mini reference. 5 Binary numbers 12

Input/Output: Advanced Concepts

Ch 11. C File Processing (review)

Introduction to file management

SOFTWARE ARCHITECTURE 2. FILE SYSTEM. Tatsuya Hagino lecture URL.

Chapter 12. Files (reference: Deitel s chap 11) chap8

fopen() fclose() fgetc() fputc() fread() fwrite()

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

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

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

CSCI 171 Chapter Outlines

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

Naked C Lecture 6. File Operations and System Calls

M.CS201 Programming language

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

Lecture6 File Processing

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

UNIX Shell. The shell sits between you and the operating system, acting as a command interpreter

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)

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

A stream is infinite. File access methods. File I/O in C++ 4. File input/output David Keil CS II 2/03. The extractor and inserter form expressions

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

The fopen command can open an external file in C language. If the file is exist, the file pointer is not NULL. Otherwise, the file pointer is NULL.

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

UNIX input and output

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

Introduction to Computer Programming Lecture 18 Binary Files

Input/Output, Libraries, and Files 2000 UW CSE

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

Lecture 03 Bits, Bytes and Data Types

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

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

CSE 333 SECTION 3. POSIX I/O Functions

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

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

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

C File Processing: One-Page Summary

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

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

Disclaimer: this is conceptually on target, but detailed implementation is likely different and/or more complex.

Chapter 10: File Input / Output

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

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

Transcription:

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

A mini Quiz 2 Consider the following struct definition struct name{ int a; float b; }; Then somewhere in main() struct name *ptr,p; ptr=&p; // Referencing pointer to memory address of p How to access variable a? (*ptr).a ptr->a

A mini Quiz 3 Consider the following code snippet: void avg_sum(double a[], int n, double *avg, double *sum) { int i; sum = 0.0; for(i = 0; i < n; i++) sum += a[i]; avg = sum / n; }

A mini Quiz 4 Consider the following code snippet: void avg_sum(double a[], int n, double *avg, double *sum) { int i; *sum = 0.0; for(i = 0; i < n; i++) *sum += a[i]; *avg = *sum/ n; / n; }

Files 5 A collection of data on disk managed by the user and the operating system A way to permanently store data A file name is how the user and OS know the filefollows OS naming rules Why (or When to) use files? Large volume of input/output data More permanent storage of data Transfer to other programs Multiple simultaneous input and/or output streams

Examples of Files 6 Scientific Data: Books Papers Reports Biological data Weather data Environmental data Web Data: html pages Images (jpg, gif, png) Videos Programs

Files and File Variables 7 Remember a file is a collection of data (on disk) But a C program can only operate directly on variables Need to make a connection between the data on the disk and variables in a C program File Variables A file variable is a data structure (FILE in <stdio.h>) which represents a file Temporary: exists only when program runs

Files in C 8 Type for file variables: FILE * Pointers to a FILE struct. File operations use functions from stdio.h fopen for opening a file fclose for closing a file getc for reading characters from a file putc for writing characters to a file fscanf for reading other data types from a file fprintf for writing other data types to files

Key Unix I/O Design Concepts 9 High Level I/O Streams Uniformity file operations, device I/O, and interprocess communication through open, read/write, close Low Level I/O syscall File System Handles Registers Descriptors Allows simple composition of programs I/O Drivers Data transferring e.g., find or grep tools Open before use Access control and arbitration Sets up the underlying machinery, i.e., data structures Explicit close

The File System Abstraction 10 High-level idea File Files live in hierarchical namespace of filenames Named collection of data in a file system File data Text, binary File Metadata: information about the file Size, modification time, owner, security information Basis for access control Directory (more in a future lecture) Folder containing files & Directories Hierarchical namespace Path uniquely identifies a file or directory

Opening and Closing Files 11 Prototype: FILE *fopen(const char *filename, const char *mode); filename: identifies file to open mode: r for reading The file must exist w for writing Creates an empty file If the file exists its contents are erased (!) a for appending To append data at the end of the file The file is created if it doesn t exist. Prototype: int fclose(file *stream);

Positioning in Files 12 File offset For input files, it gives the number of the byte to be read next It is set to zero when file is opened (using "r" mode) Offset value increases as bytes are read from file For output files, it gives the number of the byte to be written next It is set to zero when file is opened (with mode "w"). Offset value increases as bytes are written to file. For both input and output files, the current value of file offset can be obtained using the ftell function.

Library Function ftell 13 Part of stdio.h. Prototype: long ftell(file *fp) fp specifies the (input or output) file. Returns the offset for the file specified by fp Returns -1L in case of error and the global variable errno is set to a positive value

Library Function fseek 14 Part of stdio.h. Prototype: int fseek (FILE *fp, long offset, int origin) Sets the file position of the stream to the given offset fp specifies the (input or output) file offset (which may be negative) specifies the number of bytes to offset from origin Parameter origin is the position from where offset is added/subtracted SEEK_SET: Beginning of file SEEK_CUR: Current position of the file pointer SEEK_END: EOF Returns zero if successful and a non-zero value otherwise

Library Function rewind 15 Part of stdio.h. Prototype: int rewind (FILE *fp) Sets the file offset to zero (i.e., gets us back to the beginning of a file) rewind(fp) is equivalent to fseek(fp, 0, SEEK_SET); Note: Handout 4.1 illustrates the use of library functions fseek, ftell, and rewind to move around in an input file specified by a command line argument

Common Pitfalls with File Accesses 16 Moving outside file boundary Function fseek allows any offset value It doesn t check whether specified move is within the file The result of illegal moves is implementation dependent On most Unix systems: fseek does not move the offset value below the beginning of the file offset can be changed to a value beyond the end of file, however, trying to read from a non-existent position produces EOF. For an output file, fseek allows forward jumps ; positions where nothing was written contain \0.

Common Pitfalls with File Accesses 17 Forgetting to close a file The file descriptor is "leaked" The operating system associates some resources with a file descriptor that is linked to an open file Not closing the file means that the descriptor is not cleaned up, i.e., it will persist until the program closes Opening a file multiple times without closing it first more and more descriptors will be leaked Eventually the operating system either refuses or is unable to create another descriptor The call to fopen fails Accessing a file after it has been closed Segmentation fault