Programming with Data Files

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Streams and File I/O. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  I/O Streams  File I/O  Character.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
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.
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.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 3: Input/Output
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.
Chapter 12 Streams and File I/O. Learning Objectives I/O Streams – File I/O – Character I/O Tools for Stream I/O – File names as input – Formatting output,
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
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.
Chapter 3: Input/Output
Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
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.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Program Input and the Software Design Process ROBERT REAVES.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
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++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
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.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
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.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Basic Input and Output Operations
Data Streams.
Lecture 5A File processing Richard Gesick.
Basic File I/O and Stream Objects
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Chapter 3: Input/Output
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.
File I/O.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Reading from and Writing to Files
Programming with Data Files
ifstreams and ofstreams
COMS 261 Computer Science I
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:

Programming with Data Files ELEC 206 Computer Tools for Electrical Engineering

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.

Simple I/O: cin object of type istream istream class is defined in file iostream cin is also declared in iostream (istream cin;) stream input from standard input using the extraction operator (cin >> identifier) Data entered from the keyboard must be compatible with the data type of the variable. some associated member functions: eof(), get()

Simple I/O: 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 using the insertion operator (cout << expression << expression;) an expression is any C++ expression (string constant, identifier, formula or function call) some associated member functions: put()

Characters and input >> discards leading whitespace get() method used to input whitespace characters Example: int x; char y; cin >> x >> y; cin >> x; cin.get(y); x y x y 45 c 39 b 45 ‘c’ 39 ‘\n ’

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

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

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

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

#include <fstream> #include <cmath> 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(); } //end main

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)

Example - Specified # 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; }

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

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

Stringstream stringstream class is defined in file sstream provides an interface to manipulate strings as if they were input/output streams. The object of this class can be identified with iostream some associated member functions: get(), getline(), ignore(), putback(), etc. Member functions can be inherited from the iostream class Useful for converting from one arbitrary type to another