Passing Streams to Functions. Passing Streams to Functions One Rule: always pass a stream as a reference.

Slides:



Advertisements
Similar presentations
Slide: 1 Interra Induction Training Object Oriented Programming in C++ IO Streams Kolkata, July 25, 2005.
Advertisements

Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
Getting Data into Your Program
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions Prototypes, parameter passing, return values, activation frams.
1 Data Structures - CSCI 102 CS102 C++ Operator Overloading Prof Tejada.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Template Classes. A class that depends on an underlying data type(s) likewise can be implemented as a template class. We can have a ‘NewClass’ of ints,
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
File streams Chapter , ,
CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
Announcements Final Exam:. Review Passing Arrays as Parameters Pass Array Name into Function int intArray[100]; func(intArray); Accept into Function as.
1 Lab Session-VIII CSIT-121 Spring 2002 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
Introduction to Object- Oriented Programming Prof. Shie-Jue Lee Dept. of Electrical Engineering National Sun Yat-sen University.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates.
Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
ITEC 320 C++ Examples.
C++ Introduction : C++ compilation. Visual Studio 2008 : Creating Command-Line Program.
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.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements.
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Data Types Storage Size Domain of all possible values Operations 1.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
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.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
File Processing.
CS Computer Science IA: Procedural Programming
CS 1430: Programming in C++.
when need to keep permanently
Standard Input/Output Stream
CS150 Introduction to Computer Science 1
File I/O in C++ I.
CS150 Introduction to Computer Science 1
File I/O.
CS150 Introduction to Computer Science 1
Function Overloading.
CHAPTER 4 File Processing.
Reading Data From and Writing Data To Text Files
Anatomy of a class Part II
File I/O in C++ II.
COP 3330 Object-oriented Programming in C++
Lecture 9 Files Handling
Text Files.
Presentation transcript:

Passing Streams to Functions

Passing Streams to Functions One Rule: always pass a stream as a reference

file: fileopen.h // Pre: template parameter T must be either ifstream or ofstream type. template void fileopen (T & filestr, const string promptpart) { const int MAX_TRIES = 5; int count = 0; string filename; cout<<”enter name of “<<promptpart<<” file: “; cin>>filename; filestr.open(filename.c_str()); while (!filestr) { filestr.clear(); // may be necessary on your platform cout<<”ERROR: file not connected. Try again...”<<endl; cout<<”enter name of “<<promptpart<<” file: “; cin>>filename; filestr.open(filename.c_str()); count++; if (count > MAX_TRIES) { cout<<”NOT CONNECTING AFTER “<<MAX_TRIES<<” ATTEMPTS...BAILING OUT” <<”...”<<endl; exit(1); } return; }

#include #include “fileopen.h” int main() { ifstream in; fileopen(in, “input”);

Operator Overloading ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; }

Operator Overloading ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } // example 1 point p1, p2; cout << p1; cout << p1 << " “ << p2;

Operator Overloading ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } // example 1 point p1, p2; cout << p1; cout << p1 << “ ” << p2; // example 2 ofstream fout; fout << p1; fout << p1 << “ ” << p2;

Chaining // example 1 cout << p1 << “ ” << p2;... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } Stream Processing...

Chaining // example 1 cout << p1 << “ ” << p2;... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } Stream Processing... cout << p1

Chaining // example 1 cout << p1 << “ ” << p2;... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } Stream Processing... cout << p1 executes overloaded operator<<

Chaining // example 1 cout << p1 << “ ” << p2;... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream

Chaining // example 1 cout << p1 << “ ” << p2;... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << “ ”

Chaining // example 1 cout << p1 << “ ” << p2;... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } System Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << “ ” returns cout with the space added to the stream

Chaining // example 1 cout << p1 << “ ” << p2;... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << “ ” returns cout with the space added to the stream cout << p2

Chaining // example 1 cout << p1 << “ ” << p2;... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << “ ” returns cout with the space added to the stream cout << p2 executes overloaded operator<<

Chaining // example 1 cout << p1 << “ ” << p2;... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << “ ” returns cout with the space added to the stream cout << p2 executes overloaded operator<< returns cout with the points data added to the stream

Final Note  iostream and fstream are of the same family  getline, ignore, get, putback, etc are all available for filestreams as well! ifstream fin; char input; fin.open(“input.dat”); while (in.get(input)) { process_data(input);

End of Session