CS114-009 Class 13 Today  File I/O (reading from and writing to files)  Reading until EOF  Formatting output  Nested Loops Announcements  Programming.

Slides:



Advertisements
Similar presentations
Chapter 3 Reading a Set of Data. CIS 1.5 Introduction to Computing with C++3-2 Reading a Set of data A Simple Payroll Program Problem: Write a complete.
Advertisements

CS Class 16 Today Announcements Review for Exam
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.
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
I/O Streams as an Introduction to Objects and Classes
Computer Science 1620 Loops.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 Lecture 15 Chapter 6 Looping Dale/Weems/Headington.
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.
Chapter 6 Looping Dale/Weems. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
The If/Else Statement, Boolean Flags, and Menus Page 180
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Chapter 6 Looping.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
C++ Streams Lecture-2.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
File I/O ifstreams and ofstreams Sections 11.1 &
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Chapter 6 Looping. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition to.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
1 Looping. 2 Chapter 6 Topics  While Statement Syntax  Phases of Loop Execution  Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
Chapter 3: Assignment, Formatting, and Interactive Input.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Writing to Files and Reading From Files. Writing Data to a File Creating a new file, a.dat or.txt file #include #include // for file writing #include.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Announcements Midterm2 on April 18 th Monday at 19:40 This week recitations, we will solve sample midterm questions. I will also send previous years’ exams.
C++ Basic Input and Output (I/O)
Output Stream Formatting
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Let’s all Repeat Together
Chapter 4 INPUT AND OUTPUT OBJECTS
Formatted Input, Output & File Input, Output
Let’s all Repeat Together
CPS120: Introduction to Computer Science
Reading from and Writing to Files
Programming with Data Files
C++ for Engineers and Scientists Second Edition
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

CS Class 13 Today  File I/O (reading from and writing to files)  Reading until EOF  Formatting output  Nested Loops Announcements  Programming project #4: will be posted by Thursday is due by midnight Oct. 23  Exam 2 is Oct. 28 during class  Read pages , 317 – 331 for next time

Input Overview Up until now  all reads from “standard input” using cin >> …  all writes to “standard output” using cout << … Want to expand this  Read from any input  Write to any output C++ input/output uses a “stream”  Flow of characters cin : data flows into pgm cout : data flows from pgm  Can also define streams of input/output with files

Reading/Writing to files Need an identifier to use when reading from (or writing to) a file Can’t use cin and cout, they go to standard input/output ofstream outFile; ifstream inFile; Need to associate this variable with an actual file outFile.open(“results.txt”); inFile.open(“data.txt”); Same general syntax when reading/writing outFile << “answer is :” << x << endl; inFile >> x >> y; Close this file once you are done outFile.close( ); inFile.close();

Examples #include using namespace std; int main( ) { int value, temp = 1; ifstream Alabama; Alabama.open("data.txt"); while (temp < 10) { Alabama >> value; cout << "Read = " << value << endl; temp = temp + 1; } Alabama.close( ); return 0; } #include using namespace std; int main( ) { int temp = 1; ofstream bigAL; bigAL.open("results.txt"); while (temp < 10) { bigAL << temp << endl; temp = temp + 1; } bigAL.close( ); return 0; }

In-Class Exercise Write a C++ program that will read the ten numbers shown below from the file data.txt and write the even numbers to the file even.txt and the write the odd numbers to the file odd.txt You can create the input file data.txt by either 1) using Notepad 2) File / Add New Item / Make sure your input data file is in the same directory as your C++ project.

Summary: File-based I/O For each file used, need a variable that links the C++ program with a specific external file C++ Program #include #include int main( ) { ifstream inFile; ofstream outFile; inFile.open(“data.txt”); outFile.open(“results.txt”); inFile >> … ; outFile << … ; data.txt results.txt

Reading input Up until now Either Knew exactly how many input values there were  Count-controlled loops Or Knew what the last item in the data looked like  Sentinel-controlled loops What if you don’t know when the data ends (and what the last item is) ? Read until EOF Terminology: read until “end-of-file” (EOF) Two solutions  let the “read” fail  check for end-of-file How does the program see the “EOF”?  Reading from file system detects the end of the file  Reading from cin input a CTRL-d or CTRL-z

Method #2: Test for EOF #include using namespace std; int main( ) { int a; cin >> a; while ( ! cin.eof( ) ) { cout > a; } cout << "done" << endl; return 0; } // signal EOF via CTRL-z Read until you hit EOF Method #1 : Let read fail #include using namespace std; int main( ) { int a; while ( cin >> a ) { cout << a << endl; } cout << "done" << endl; return 0; } // signal EOF via CTRL-d

Class Exercise Write a C++ program that reads integers from standard input until EOF, printing the total and the average value once the user enters EOF. Your average value should be a double. Modify your program to read from the file data.txt and output to the screen.

Formatting Output Program output is “presented” to the user. Want professional look  Alignment  justification All output streams can be formatted.  cout  File output streams Specify flags for presentation  Field width  Precision  Left justify  Right justify  Scientific notation

Formatting example e Comments  Width only for next output item  Will print entire number if field is too small int num0 = 16; int num1 = 1024; cout.width(5); cout << num0 << endl; cout << num1 << endl; cout.width(8); cout.setf(ios::right); cout << num0 << endl; cout << num1 << endl; cout.width(2); cout << num1 << endl; double num2 = ; cout.setf(ios::scientific); cout << num2 << endl; cout.setf(ios::fixed); cout << num2 << endl; cout.precision(2); cout << num2 << endl;

Rules for Formatting Just specify the flags needed to give you the desired format Can “unset” these flags later on (if needed) cout.unsetf(ios::showpos); Lots of flags exist. Examples  Print integers right- justified and always show the “+” sign cout.setf(ios::right); cout.setf(ios::showpos);  Print doubles using scientific notation cout.setf(ios::scientific);  Print next integer in a field of width 8 cout.width(8);

Basic formatting alternatives Integer Values  Show “+” w/positive num setf(ios::showpos)  Specifies width (only for the next value output) width(N)  left-justify output setf(ios::left)  right-justify output setf(ios::right) Real Values  Fixed decimal notation setf(ios::fixed)  Scientific notation setf(ios::scientific)  Show decimal point setf(ios::showpoint)  Show “+” w/positive num setf(ios::showpos)  Set decimal places precision(N)

Class Exercises – Trace then run int main( ) { int a = 5, loop = 3; while (loop <= 8) { cout.width(loop); cout.setf(ios::right); if (loop%2 == 0) cout.setf(ios::showpos); else cout.unsetf(ios::showpos); cout << a << endl; loop = loop + 1; a = a * 2; } return 0; } int main( ) { double z= ; int loop = 3; while (loop < 8) { cout.precision(loop); if (loop == 6) cout.setf(ios::left); cout.width(15-loop); cout << z << endl; loop = loop + 1; } return 0; }

Class Exercises Write a program that generates the output below: Write a program that generates the output below:

Nesting - Review Remember: If and while statements can be nested inside each other Can nest as deeply as needed by algorithm Can nest in any order

Example: multiplication table #include using namespace std; int main( ) { for (int a=1; a<6; a++) { for (int b=1; b<6; b++) { cout.width(4); cout << a*b; } cout << endl; } return 0; }

Example: powers, up to N N #include using namespace std; int main( ) { for (int a=1; a<6; a++) { for (int b=1; b<=a; b++) { cout.width(5); cout << pow(a,b); } cout << endl; } return 0; }

Write C++ programs that use nested for loops to display one of the following designs: XXXXXX XXXXXXXXXX XXXXX XXXXX XXXXXXXX X X XXXX XXXXXX X X XXX XXXX X X XX XX X X Class Exercise

End of Class 13