Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
Advertisements

File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
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.
© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
Chapter 7: Files Mr. Dave Clausen La Cañada High School.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
Chapter 5: Loops and Files.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
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.
Chapter 15.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
Program Input and the Software Design Process ROBERT REAVES.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
Chapter 8 Data File Basics.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
C++ Introduction : C++ compilation. Visual Studio 2008 : Creating Command-Line Program.
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1-5.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Program Input and the Software Design Process ROBERT REAVES.
1 Chapter 4 Program Input and the Software Design Process.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
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.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 14: Sequential Access Files
Lecture 3 C & C++ programming.
Lecture 9 Files, Pointers
Chapter 5: Looping Starting Out with C++ Early Objects Eighth Edition
Interactive I/O Input from keyboard Must prompt user User friendly
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Expressions and Interactivity
Chapter 12: Advanced File Operations.
File I/O in C++ I.
Formatted Input, Output & File Input, Output
CS150 Introduction to Computer Science 1
Standard Version of Starting Out with C++, 4th Edition
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
Programming with Data Files
Reading Data From and Writing Data To Text Files
Lecture 9 Files Handling
File I/O in C++ I.
Presentation transcript:

Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming

Introduction to File Input and Output

Can use files instead of keyboard, monitor screen for program input, output Allows data to be retained between program runs Steps: Open the file Use the file (read from, write to, or both) Close the file

Files: What is Needed Use fstream header file for file access File stream types: ifstream for input from a file ofstream for output to a file fstream for input from or output to a file Define file stream objects: ifstream infile; ofstream outfile;

Opening Files Create a link between file name (outside the program) and file stream object (inside the program) Use the open member function: infile.open("inventory.dat"); outfile.open("report.txt"); Filename may include drive, path info. Output file will be created if necessary; existing file will be erased first Input file must exist for open to work

Writing Data into a file Insertion operator (<<) with cout Can use output file stream object and << to send data to a file: outfile << “Happy Birthday!";

Reading data from a file Stream extraction operator >> Can use input file object and >> to copy data from file to variables: infile >> studentName; infile >> studentSSN >> studentGPA;

Closing Files Use the close member function: infile.close(); outfile.close(); Don’t wait for operating system to close files at program end: may be limit on number of open files may be buffered output data waiting to send to file

Example Read jokes line by line from the text file jokes.txt and display them on the screen.

Hand Tracing a Program Hand trace a program: act as if you are the computer, executing a program: step through and ‘execute’ each statement, one-by-one record the contents of variables after statement execution, using a hand trace chart (table) Useful to locate logic or mathematical errors