1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.

Slides:



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

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.
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 * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
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.
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.
Chapter 5: Loops and Files.
1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.
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.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Arithmetic Operators Operation Operator Example Addition = 9 Subtraction = 1 and = -1 Multiplication * 5 * 4 = 9 Division (integer)
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates.
Chapter 8 Data File Basics.
CS Class 13 Today  File I/O (reading from and writing to files)  Reading until EOF  Formatting output  Nested Loops Announcements  Programming.
File I/O ifstreams and ofstreams Sections 11.1 &
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.
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.
COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
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.
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.
Chapter 14: Sequential Access Files
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
Quiz Next Monday.
Data Streams.
File I/O.
File I/O.
More About File Reading
Basic File I/O and Stream 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.
File I/O.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
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
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

1 Programming with Data Files Chapter 4

2 Standard Input Output C++ Program Keyboard input cin Output Screen cout

3 File Input / Output features C++ Program Keyboard input cin Output Screen cout ifstream infile; ofstream outfile;

4 Programmer Defined File Streams To define your own file streams  for input use ifstream class Ifstream: input file stream  for output use ofstream class Ofstream: output file stream ifstream and ofstream are defined in package fstream (file stream) Files provide “persistence” or permanent copy for the results generated by your programs.

5 File Operations Open, close, > operators eof() operation on an input file object returns a true or false (Boolean) get() reads a single character from an input file and put(char) writes a single character into an output file.

6 Defining File Streams 1. Include fstream (#include ) 2. declare file stream variable (object) 1. ifstream fin; 2. ofstream fout; 3. use open() to initialize file stream variable 4. use input file stream variable as you would use cin and use output file stream variable as you would use cout 5. use close() to close the file when finished with it

7 #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

8 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

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

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

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

12 Summary We looked at  Standard Input and Output  File Input and output features  Programmer Defined Files  File operations  Reading multiple data sets from input file  Data recorded in the files can be used for analysis using other software packages such as Matlab