lecture17-mar2 - Announcements Lecture 17 Assignment 3...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Announcements Lecture 17 • Assignment 3 – write down your stats before you forget them • SLOCs, hours, logic defects • Assignment 4 – out today – Q&A – Designs due in recitation Friday • IPO, functional block diagram, algorithm (high level) • Topics for today – start File IO (Chapter 22) • Files and streams • Text IO operations Files A file is — 1. A named collection of related data stored on a secondary storage device , such as disk, diskette, flash stick, CD, etc. • Permanent — data survives indefinitely. 2. Most OS’s support two different types of files: text and binary Text files are stored as a sequence of lines; with end of line markers Binary files are not Files on here: e.g. •tfgrading.c •assn2data.txt •myresume.doc •mypgm.exe Basic Operations on Files • Open – in a specific mode • Read – usually sequentially • Write – usually sequentially • Close file1 EOF Disk Name Access mode Size Current location ptr File descriptor FILE *fp; text files and binary files are stored differently The data is converted to/from stream format when transferred (e.g. buffered) Streams A stream is an abstraction of the continuous one-way flow of data between a program and an external device (e.g., keyboard or screen) or files on a secondary storage device (e.g. disk). A stream is categorized into one of two types: binary (byte) stream or character (text) stream . They transfer one data element at a time (a byte or character) in sequential order. Binary streams contain non printable bit patterns. Program In memory Output Stream File on disk Input Stream File Pointers and Streams Declarations of functions that perform file I/O appear in <stdio.h>. Each function requires a file pointer as a parameter. A file pointer is a pointer to a FILE description structure, it is declared as: FILE *fp; /* fp holds a pointer to a file descriptor */ A program may declare as many file pointers as needed A file pointer represents a stream, which may be a file or—in general—any source of input or output. Three streams are standard: stdin Standard input (keyboard) stdout Standard output (screen) stderr Standard error (different window on the screen) These streams need not be explicitly opened or closed by you. Standard input and output can be redirected in both UNIX and Windows from the command line: prog <data >result UNIX also allows redirection of the standard error stream. Only certain versions of Windows (NT and 2000) allow this. Redirection of standard error is done by using >> instead of >. Opening and Closing Files
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

lecture17-mar2 - Announcements Lecture 17 Assignment 3...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online