1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and.
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.
Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
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.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
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.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
File I/O ifstreams and ofstreams Sections 11.1 &
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
Practice Building Classes Modeling Objects. Problem Write a program that computes the Dean’s List (full-time students whose GPA 3.0), using a student.
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
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 Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
File Handling in C++.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Lecture 14 Arguments, Classes and Files. Arguments.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Functions Manipulating Files
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Quiz Next Monday.
File I/O.
Lecture 5A File processing Richard Gesick.
File I/O.
Working with Data Files
More About File Reading
Basic File I/O and Stream Objects
File Streams and File I/O
Starting Out with C++: From Control Structures through Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O in C++ I.
CS150 Introduction to Computer Science 1
File I/O.
CHAPTER 4 File Processing.
Reading from and Writing to Files
Programming with Data Files
ifstreams and ofstreams
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
File I/O in C++ II.
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files

2 Object Oriented Programming A class is a mechanism that allows a programmer to define new data types. A class can be used to add functionality to an existing data type or to create a new data type. A class definition combines data and functionality. An object is a variable of a defined class type, also referred to as an instance of a class.

3 File Streams cin –object of type istream –istream class is defined in file iostream –cin is also declared in iostream (istream cin;) defined to stream input from standard input some associated member functions: –eof() –get()

4 File Streams cout –object of type ostream –ostream is defined in the file iostream –cout is also declared in iostream (ostream cout;) cout is defined to stream output to standard output some associated member functions: –put()

5 Programmer Defined File Streams To define your own file streams – for input use ifstream class – for output use ofstream class ifstream and ofstream are defined in file fstream

6 File Streams ifstream is derived from istream includes all functions defined for cin - eof(), get(),.. Includes additional functions not defined for cin - open(), close()

7 File Streams ofstream is derived from ostream includes all functions defined for cout - put(),.. Includes additional functions not defined for cout - open(), close()

8 Defining File Streams Include fstream declare file stream variable (object) –ifstream fin; –ofstream fout; use open() to initialize file stream variable use file stream variable as you would use cin and cout use close() to close the file when finished with it

9 #include using namespace std; int main() { //Create three columns of data for plotting. double x; ifstream xdata; //declare input stream ofstream plotdata; //declare output stream xdata.open("data1"); //xdata uses file data1 if( xdata.fail() ) //check for error { cout << "error opening input file" << endl; return 0; } //end if fail plotdata.open("plot1");//plotdata uses file plot1 xdata >> x; //input x from file while(!xdata.eof()) //while not end of file { if( x>0 ) //write to plot file plotdata<<x<<" "<<exp(x)<<" "<<log(x)<<endl; xdata >> x; //get next data point } //end while xdata.close(); plotdata.close(); return 0; } //end main

10 Reading Data Files Specified number of records for loop Trailer or Sentinel Signal while loop Data records only (no specified number of records, no sentinel value) while loop

11 Example - Specified Number of Records … int main() { fstream fin(“data”); double x; int num_data_points; … fin >> num_data_points; for(int i=0; i<num_data_points; i++) { fin >> x; … } …

12 Example - Sentinel Signal const double sentinelValue = -99; … int main() { fstream fin(“data”); double x; … fin >> x; while(x != sentinelValue) { … fin >> x; } …

13 Example - No Specified Number of Records, no Sentinel Signal … int main() { fstream fin(“data”); double x; … fin >> x; while( !fin.eof() ) { … fin >> x; } …