 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
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.
Arithmetic Operators Operation Operator Example Addition = 9 Subtraction = 1 and = -1 Multiplication * 5 * 4 = 9 Division (integer)
Chapter 8 Data File Basics.
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
1 I/O  C++ has no built-in support for input/output input/output is a library (iostream)  C++ program views input and output as a stream of bytes  Input:
COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Practice Building Classes Modeling Objects. Problem Write a program that computes the Dean’s List (full-time students whose GPA 3.0), using a student.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
C++: Functions, Program Compilation, Libraries Modified from CS101 slides, which are by JPC and JWD © 2002 McGraw-Hill, Inc.
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
ifstreams and ofstreams
What Actions Do We Have Part 1
Introduction to Programming
Data Streams.
Lecture 5A File processing Richard Gesick.
Basic File I/O and Stream Objects
IO Overview CSCE 121 J. Michael Moore
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
File I/O in C++ I.
CS150 Introduction to Computer Science 1
File I/O.
Reading from and Writing to Files
Programming with Data Files
ifstreams and ofstreams
IO Overview CSCE 121 Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch.
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output to files  Reading: 5.5,  Exercises: p. 205 #13, 20  New files/handouts: FileAverage.cpp, InData.txt, CheckFile.h

 Wednesday, 10/16/02, Slide #2 Library Library  The library provides objects and functions that let us do input and output from/to the computer keyboard/monitor  Objects/ types:  cin -- the keyboard input stream, type istream  cout -- the monitor output stream, type ostream  cerr -- the monitor error output stream, type ostream  Functions/operators:  >> extraction operator  << insertion operator  Other I/O functions such as get(), put(), peek(), etc.

 Wednesday, 10/16/02, Slide #3 Library Library  The library gives us classes that permit us to declare file types, and do file operations.  Our programs can then get input data from a file and send output data to a file.  Types provided:  ifstream -- a type for declaring input files  ofstream -- a type for declaring output files  Functions/operators:  >> input file extraction operator  << output file insertion operator  open() and close(): functions to begin/end associating a file object with an actual file name

 Wednesday, 10/16/02, Slide #4 Using files: See FileAverage.cpp  1. Declare a file object:  ifstream fin; //name can be anything you want  ofstream fout; //but fin and fout are very standard  2. “Open” the file:  fin.open(“C:\\CS106\\InData.txt”); //Note double quotes  fout.open(“C:\\CS106\\OutData.txt”); //and double backslashes  3. Use the file:  fin >> num; //where num is, say, an int object  fout << num;  4. Close the file:  fin.close();  fout.close();

 Wednesday, 10/16/02, Slide #5 More on using files  Program treats input file just like keyboard, processing characters in order they appear  Input file data must be separated by spaces or line feeds (not commas, dashes, etc.)  Sometimes hard to get the pathname & filename right -- program tries to input from empty file  Function CheckFile() (in CheckFile.h) checks an input file, and prints error message if opened file was not found or empty.  When you open and begin outputting to a file, anything that was previously in the file is erased!

 Wednesday, 10/16/02, Slide #6 Getting file input in loops  When we use the extraction operator, it returns a true/false value that depends on whether it successfully/unsuccessfully extracted a value  We can use this value as a loop condition while (fin >> Value) { cout << Value << endl; ++ValuesProcessed; ValueSum += Value; }