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

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

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

C Programming Language

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

System Software Experiment 1 Lecture 7

Intermediate Programming, Spring 2017*

Week 9 Lecture 3. Binary Files. Week 9

File Handling. Reference:

Computer System and programming in C

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

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

Computer Programming Unit v

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

25.2 Opening and Closing a File

C-Refresher: Session 10 Disk IO

Computer programming

CS240: Programming in C

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

ENG120. Misc. Topics

Input/Output and the Operating Systems

Data File and File Handling

Introduction to Computer Programming Lecture 18 Binary Files

2009 S2 COMP File Operations

Lecture 9: File Processing. Quazi Rahman

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

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

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

PROGRAMMAZIONE I A.A. 2017/2018

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

Chapter 11 File Processing

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

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

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

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

Fundamentals of Programming. Lecture 10 Hamed Rasifard

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING UNIT-1

EM108 Software Development for Engineers

Chapter 12. Text and Binary File Processing. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

UNIX System Programming

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

Input/Output: Advanced Concepts

Input / Output Functions

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING

Ch 11. C File Processing (review)

UNIT- 2. Binary File

C File Processing: One-Page Summary

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Lecture6 File Processing

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

C PROGRAMMING. Characters and Strings File Processing Exercise

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

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

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

File I/O. Preprocessor Macros

Standard C Library Functions

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

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

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

CSCE C. Lab 10 - File I/O. Dr. Chris Bourke

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

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

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

C/C++ Text File Functions

A Crash Course in C. Steven Reeves

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

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

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

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

Lecture 17 Bit Operations

UNIX input and output

File Input/Output. Similarly, for reading from files and writing to files, we'll use the functions

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

Lecture 7: file I/O, more Unix

Introduction to file management

C mini reference. 5 Binary numbers 12

IO = Input & Output 2

C Basics And Concepts Input And Output

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

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

HIGH LEVEL FILE PROCESSING

Engineering program development 7. Edited by Péter Vass

C: How to Program. Week /June/18

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

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.

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

CS240: Programming in C

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

Lecture 8: Structs & File I/O

Library Functions. General Questions

Advanced C Programming Topics

STRUCTURES & FILE IO

mywbut.com 12 File Input/Output

CSCI 171 Chapter Outlines

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

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

Program Design (II): Quiz2 May 18, 2009 Part1. True/False Questions (30pts) Part2. Multiple Choice Questions (40pts)

Appendix A Developing a C Program on the UNIX system

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

Data Files. Computer Basics

Transcription:

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

Files A Quick Review Type for file variables: FILE * File operations use functions from stdio.h. Functions fopen and fclose for opening and closing files. Functions getc and putc for reading and writing characters from/to files. Functions fscanf and fprintf for reading and writing other data types from/to files. 2 2 / 20

Positioning in Files Ref: Chapter 11 of Deitel & Deitel. 0 1 2 3 4 EOF... File Offset Input files: File offset gives the number of the byte to be read next. It is set to zero when file is opened (using "r" mode). Value of file offset increases as bytes are read from file. 2 3 / 20

Positioning in Files (continued) Output files: File offset gives the number of the byte to be written next. It is set to zero when file is opened (with mode "w"). File offset increases as bytes are written to file. Note: For both input and output files, the current value of file offset can be obtained using ftell function. 2 4 / 20

Library Function ftell Part of stdio.h. Prototype: long ftell(file *fp) Returns the offset for the file specified by fp; returns -1L in case of error. Example: FILE *fp; long pos;.. /* Open file, etc. */.. pos = ftell(fp); printf("offset = %ld\n", pos); 2 5 / 20

Library Function fseek Also part of stdio.h. To move around in a file. Prototype: int fseek (FILE *fp, long offset, int origin) fp specifies the (input or output) file. offset (which may be negative) specifies the amount of movement. How offset is used depends on the parameter origin. 2 6 / 20

Library Function fseek (continued) Parameter origin can have any of the following three values (constants). SEEK_SET: offset specified relative to the beginning of the file. SEEK_CUR: offset specified relative to the current position. SEEK_END: offset specified relative to the end of the file. Function fseek returns 0 if successful and a non-zero value otherwise. 2 7 / 20

A Related Function: rewind Part of stdio.h. Prototype: int rewind (FILE *fp) Sets file offset to 0 (i.e., gets us back to the beginning of a file). rewind(fp) is equivalent to fseek(fp, 0L, SEEK_SET); Program Example: Handout 2.1. 2 8 / 20

Moving Outside File Boundary Function fseek allows any offset value; it doesn t check whether specified move is within the file. For illegal moves, effect is implementation dependent. On most Unix systems: Function fseek does not move the offset value below the beginning of the file. 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. 2 9 / 20

Random Access Files Random Access: Example: For files: Access time is independent of position. Array : Provides random access. List : Does not provide random access. (Provides sequential access.) Random access: Fast access. Applications: Airline reservation systems, Banking systems, etc. 2 10 / 20

Random Access Files in C No explicit support. (Functions fread and fwrite from stdio.h are used.) Common method: Make all records to be of the same size. Example: 0 50 100... 1 2 3 Size of each record = 50 bytes Starting position of Record i = (i 1) Size of record. 2 11 / 20

Formatted and Unformatted Files Formatted Files: Also called text files; they can be viewed/edited using a standard text editor. Can be produced by a C program using formatted write (i.e., using fprintf). Example for formatted write: FILE *ofp; int num = -25;.. fprintf(ofp, "%d", num); No. of bytes written to the file = 3. Note: The number of bytes written to the output file depends on the value of the integer. 2 12 / 20

Formatted and Unformatted Files (continued) Unformatted files: Also called binary files; they cannot be viewed/edited using standard text editors. To produce unformatted files, C program must use unformatted write using the fwrite function. Function fwrite: Prototype: size_t fwrite (const void *p, size_t size, size_t nent, FILE *fp) Writes bytes from memory to a file. p: Gives the starting address in memory. 2 13 / 20

Description of fwrite (continued) size: nent: Gives the size (i.e., number of bytes) of each entry. Gives the number of entries to be written. fp: Pointer to the output file. Writes the specified number of entries (starting from the specified memory address) to the output file. Returns the number of entries written. (If this value is less than nent, it is an indication of error.) 2 14 / 20

Description of fwrite (continued) Some Technicalities: FILE *ofp; int num = -25; &num: Starting address of num (Type: int *). (const void *) &num: Type casts address to const void *. sizeof(num): Size of the entry (i.e., no. of bytes) to be written. No. of entries to be written: 1. Now, the call to fwrite is as follows: fwrite((const void *) &num, sizeof(num), 1, ofp); Note: We must check the return value of fwrite to ensure that no errors occurred. 2 15 / 20

Difference Between fprintf and fwrite Example: (Assume int uses 4 bytes.) int num = -2017; FILE *out_f1, *out_f2; -- Open file out_f1 (out1.fmt) -- -- Open file out_f2 (out2.ufmt) -- /* Formatted write. */ fprintf(out_f1, "%d", num); /* Unformatted write. */ fwrite((const void *) &num, sizeof(num), 1, out_f2); -- Close files. -- File out1.fmt: Size = 5 bytes. A text file: can be examined/edited using a text editor. Can be read using fscanf. 2 16 / 20

Difference Between fprintf and fwrite (continued) File out2.ufmt: Size = 4 bytes. A binary file: cannot be examined using a text editor. Can be read from using fread (a function for reading unformatted files). Formatted Read: Uses fscanf. FILE *ifp; int num;.. fscanf(ifp, "%d", &num); Unformatted read: Uses fread. 2 17 / 20

Description of Function fread Prototype: size_t fread (void *p, size_t size, size_t nent, FILE *fp) Reads bytes from file into memory. p: Gives the starting address for reading into memory. size: read. nent: fp: Gives the size (i.e., number of bytes) of each entry to be Gives the number of entries to be read. Pointer to the input file. Reads the specified number of entries from the input file into memory starting from the specified memory address. Returns the number of entries read. (If this value is less than nent, it is an indication of error.) 2 18 / 20

Program Examples 1 Creating a random access file: Handout 2.2. 2 Writing to a random access file: Handout 2.3. 3 Reading from a random access file: Handout 2.4. Examples of Binary Files: Compiled versions of C programs (i.e., files with extension.o ). Executable versions of C programs (e.g. file a.out ). Compressed files. File archives (e.g. files created using tar command in Unix). 2 19 / 20

Suggested Exercises 1 Study Handout 2.1 carefully to understand the use of functions fseek and ftell. 2 Study Handouts 2.2, 2.3 and 2.4 carefully to understand the use of functions fread and fwrite. 3 Study the other program examples in Chapter 11 of Deitel & Deitel. 2 20 / 20