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

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
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 (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.
File I/O Supplemental Material. Background In C++, files can be manipulated in the same manner we manipulate streams such as: cout and cin. Therefore,
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 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.
Chapter 8 Data File Basics.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
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.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
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 © 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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
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?
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
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.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
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:
Quiz Next Monday.
Data Streams.
Lecture 5A File processing Richard Gesick.
File I/O.
Interactive I/O Input from keyboard Must prompt user User friendly
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.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
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.
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
Reading Data From and Writing Data To Text Files
ifstreams and ofstreams
Lecture 9 Files Handling
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

Programming with Data Files Chapter 4

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

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

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.

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. fail() operation indicates if the opening of a file is successful or failure. Return Boolean type (true or false)

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

Writing Output to a File Similar to writing to screen Use object connected to output file Need the fstream header #include Open file for writing –Declare object of ofstream class ofstream outfile; Lesson 4.2

Opening Files General form outfile.open(“file_name”); Choose object_name like variable name object_name is object of class ofstream Filename is where output will be stored Ex: outfile.open(“grades.out”); Lesson 4.2

Writing to Files General form object_name << variable_name; Use ofstream object to write to file like cout was used outfile << “Salary for week was ” << money; Additional writing appended to file Lesson 4.2

Closing Files (input and output) General form object_name.close ( ); Use C++ library function close Use both object and function name separated by a period Example: outfile.close( ); Lesson 4.2

Open File for Reading Need fstream header #include Declare object of ifstream ifstream infile; Open the file: infile.open(“points.dat”); Use ifstream object to read file like cin Lesson 4.3

Reading From a File Use ifstream object to read file like cin used for keyboard infile >> salary1 >> salary2; C++ looks for whitespace between numbers –Newline character treated as whitespace Additional reading continues sequentially Lesson 4.3

Reading From a File Lesson money.dat infile >> s1 >> s2 >> s3; s1s2s s4 3.5 infile >> s4; Note: The number of data entries and their data types of variables should read should match the number and order within the file!

A complete example Open an input file “data1” Open an output file “plot1” Read x from input file Write x, exp(x) and log(x) to output file Close the input and output files

#include using namespace std; int main() { double x; ifstream xdata; //declare input stream ofstream plotdata; //declare output stream xdata.open("data1"); //xdata uses file data1 plotdata.open("plot1");//plotdata uses file plot1 xdata >> x; //input x from file plotdata<<x<<" "<<exp(x)<<" "<<log(x)<<endl; xdata.close(); plotdata.close(); return 0; } //end main