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

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

Embed Size (px)

Citation preview

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

Chapter 3 Interactivity and Expressions

CSC 125 Introduction to C++ programming

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

Introduction to File Input and Output

Page 3: 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 fileUse the file (read from, write to, or both)Close the file

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

Files: What is Needed

Use fstream header file for file accessFile stream types:

ifstream for input from a fileofstream for output to a filefstream for input from or output to a file

Define file stream objects:ifstream infile;ofstream outfile;

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

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

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

Writing Data into a file

Insertion operator (<<) with coutCan use output file stream object and <<

to send data to a file:

outfile << “Happy Birthday!";

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

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;

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

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 filesmay be buffered output data waiting to send

to file

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

Example

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

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

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