1 / 10

Chapter 3 Interactivity and Expressions

Learn how to use files instead of keyboard and monitor screen for program input and output. This allows data to be retained between program runs. Understand the steps involved in opening, using, and closing files, as well as how to read and write data from/to a file.

vgolden
Download Presentation

Chapter 3 Interactivity and Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Introduction to File Input and Output

  3. 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

  4. 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;

  5. 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

  6. 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!";

  7. 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;

  8. 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

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

  10. 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

More Related