Input Streams “A program designed for inputs from people is usually stressed beyond the breaking point by computer-generated inputs.” Dennis Ritchie, Bell.

Slides:



Advertisements
Similar presentations
Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
COMP234 Perl Printing Special Quotes File Handling.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
Interactive Session on Project 2 & Assorted Topics Copyright, 1996 © Dale Carnegie & Associates, Inc. By Surendra Singhi Spring 2005.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Program Input and the Software Design Process ROBERT REAVES.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
CS0007: Introduction to Computer Programming File IO and Recursion.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
Sed sed is a program used for editing data. It stands for stream editor. Unlike ed, sed cannot be used interactively. However, its commands are similar.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
(Stream Editor) By: Ross Mills.  Sed is an acronym for stream editor  Instead of altering the original file, sed is used to scan the input file line.
Course A201: Introduction to Programming 12/9/2010.
06 INPUT AND OUTPUT Functional Programming. Streams Two kinds of streams  Character streams  Binary streams Character streams are Lisp objects representing.
File I/O ifstreams and ofstreams Sections 11.1 &
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
Input and Output in Scheme CS 480/680 – Comparative Languages.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
File IO and command line input CSE 2451 Rong Shi.
1 Lesson Three. 2 Opening a Document Inserting Text Deleting a Character Deleting a Word Deleting a Sentence Automatic Braille Advance.
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Files Tutor: You will need ….
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
21-October-2002cse IO © 2002 University of Washington1 Input / Output CSE 413, Autumn 2002 Programming Languages
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
PRACTICAL COMMON LISP Peter Seibel 1.
Lakshit Dhanda. Output Formatting Python has ways to convert any value to a string. 2 Methods repr() – meant to generate representations of values read.
Input/Output Chapters 8 & 9. Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Python’s input and output
Plan for the Day: I/O (beyond scanf and printf)
I/O Basics.
Standard Input/Output Streams
Standard Input/Output Streams
“The fastest I/O is no I/O.” Nils-Peter Nelson, Bell Labs
Topics Introduction to File Input and Output
File Input and Output.
Perl I/O Learning Objectives:
Nate Brunelle Today: Functions
Topics Introduction to File Input and Output
File Input and Output.
Topics Introduction to File Input and Output
How to read from a file read, readline, reader
Presentation transcript:

Input Streams “A program designed for inputs from people is usually stressed beyond the breaking point by computer-generated inputs.” Dennis Ritchie, Bell Labs

Reading Primary reading function is read Reads one S-expression from the currently selected input device and returns it as a value Only uses first expression >(read) 99 >(read) (a b c) (A B C) >(read) > 2 > 3

Read Example (1) >(defun my-square () (format t "Please type a number: ") (let ((x (read))) (format t "The number ~A squared is ~A.~%" x (* x x)))) MY-SQUARE >(my-square) Please type a number: 2 The number 2 squared is 4. NIL

Read Example (2) >(defun wage-calc () (format t "Please enter your hourly wage: ") (let ((wage (read))) (format t "Please enter hours worked: ") (let ((hours (read))) (format t "Your pay is: $~$" (* wage hours))))) WAGE-CALC >(wage-calc) Please enter your hourly wage: 3 Please enter hours worked: 5 Your pay is: $15.00 NIL

Other Read Functions read-char reads any character, including newlines – Returns character read read-line reads an entire line up to and including the newline character – Returns a string with the characters read, not including the newline

Peek-char You can look at the next character in a stream without advancing the stream (peek-char mode stream) If mode is nil, then you always get the next character in the stream If mode is t, then peek-char will actually advance the stream past white space to peek at the next character that’s not white space

String Input Stream make-string-input-string converts a string into an input stream >(defvar str (make-string-input-stream "one two three")) STR >(read-char str) #\o >(read-char str) #\n >(read-char str) #\e >(read str) TWO >(peek-char nil str) #\Space >(read-char str) #\Space >(peek-char nil str) #\Space >(peek-char t str) #\t >(read str) THREE

End of Stream listen tests whether there is an available character in a stream By default, all read functions signal an error if there is nothing left to read Giving these read functions a second argument of nil makes them return NIL instead >(read (make-string-input-stream "") nil) NIL Giving yet another argument allows you to specify what value to return when the end of the stream is hit >(read (make-string-input-stream "") nil 'EOS) EOS

More Input Stream Functions clear-input flushes the contents of a stream open-stream-p tests if a stream is open close turns off a stream

File IO Open streams to files using the open function (open stream-name :direction :input) (open stream-name :direction :output) open returns a stream stream-name is usually a quoted file path probe-file checks to see if a file exists

File IO Example >(probe-file "test.txt") NIL >(defvar str (open "test.txt" :direction :output)) STR >(probe-file "test.txt") #p"/v/filer4b/v20q001/schrum2/test.txt" >(format str "We can write anything!~%") NIL >(close str) T

With-Open-File Remembering to close open streams can be cumbersome, so there is a shortcut (with-open-file (sym filename) S-expr*) Automatically closes the file when control leaves the expression Can take a :direction of :input or :output Can also specify what to do if file already exists, e.g. :if-exists :supersede

With-Open-File Example (defun print-name-help (str name) (format str "My name is ~A" name)) (defun print-name (filename) (with-open-file (str filename :direction :output :if-exists :supersede) (format t "Enter your name: ") (let ((name (read))) (print-name-help str name))))