Reading from and Writing to Files Part 2

Slides:



Advertisements
Similar presentations
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Advertisements

What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
CS150 Introduction to Computer Science 1
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
Programming is instructing a computer to perform a task for you with the help of a programming language.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
First steps Jordi Cortadella Department of Computer Science.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
1 10/15/04CS150 Introduction to Computer Science 1 Reading from and Writing to Files Part 2.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Computing Fundamentals
Chapter 2 part #1 C++ Program Structure
Introduction to C++ October 2, 2017.
CS149D Elements of Computer Science
Screen output // Definition and use of variables
Today’s Lecture I/O Streams Tools for File I/O
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
switch Selection Structure
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
Arithmetic Operations
do/while Selection Structure
What Actions Do We Have Part 1
CS150 Introduction to Computer Science 1
CS250 Introduction to Computer Science II
Reading from and Writing to Files
CS150 Introduction to Computer Science 1
Life is Full of Alternatives
CS150 Introduction to Computer Science 1
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Functions Divide and Conquer
Introduction to Programming - 1
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
Presentation transcript:

Reading from and Writing to Files Part 2 10/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Time We Started looking at how we read from and write to files Today we will Look at more examples 10/13/04 CS150 Introduction to Computer Science 1

Example: Reading from a File Write a program that will read in a sequence of numbers (double) from a file and calculate the sum. Assume that the last number is the trailer (-9999) #include<fstream> #include<iostream> using namespace std; int main() { ifstream inInfo; double num, sum = 0; inInfo.open("in.dat", ios::in); if (!inInfo) cout << "*** Error opening file" << endl; exit (1); } inInfo >> num; while ( num != -9999 ) cout << num << " "; sum += num; cout << endl << "The sum of the above numbers is " << sum << endl; return 0; 10/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Reading Until the EOF It is possible to read from a file until the end is reached while ( inInfo >> num ) { cout << num << " "; sum += num; } 10/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Reading Characters Write a program that reads in some text from a file and outputs that text to the screen The file contains: Hello Everyone! I'm a file that contains some text. 10/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Solution ifstream inInfo; char letter; inInfo.open("in.dat", ios::in); if (!inInfo) { cout << "*** Error opening file" << endl; exit (1); } while ( inInfo >> letter ) cout << letter; cout << endl; 10/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 The Output HelloEveryone!I'mafilethatcontainssometext. What’s happened?! All spaces, tabs, and new lines have been ignored. This is because >> only reads visible characters How can we read all characters so that the output looks exactly like the input 10/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Solution ifstream inInfo; char letter; inInfo.open("in.dat", ios::in); if (!inInfo) { cout << "*** Error opening file" << endl; exit (1); } while ( inInfo.get( letter ) ) cout << letter; cout << endl; 10/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Consider the data file below, where - indicate spaces: --12--33.4 -d--12.3 -2--5 What values would be assigned to the variables for each of the statements below where inInfo is the file variable? int i,j; double x,y; char ch; inInfo >> i >> x >> y; inInfo >> i >> j; inInfo >> ch >> i; inInfo >> x >> y >> ch >> x; 12 33.4 -1.99962 12 33 1 2 12.0 33.4 d 12.3 #include<fstream> #include<iostream> using namespace std; int main() { ifstream inInfo; int i, j; double x, y; char ch; inInfo.open("in.dat", ios::in); if (!inInfo) cout << "*** Error opening file" << endl; exit (1); } inInfo >> i >> x >> y; cout << i << endl << x << endl << y << endl; return 0; 10/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem A file contains the following data: 1234 500.00 041001 W 25.00 041015 W 99.25 041030 D 100.00 000000 Where the first line is a bank account number and the amount of money in the account subsequent lines contain a date, a W or D indicating withdraw and deposit, and the amount withdrawn or deposited final line contains 000000 10/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Output Write a program that will use the data in the file to output the following: 10/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered Reading to and writing from files Readings P. 809 - 819 10/13/04 CS150 Introduction to Computer Science 1