File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.

Slides:



Advertisements
Similar presentations
File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and.
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.
Functions, Projects, and C++ I/O Streams Dr. Nancy Warter-Perez June 4, 2003.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
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.
 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.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Program Input and the Software Design Process ROBERT REAVES.
The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Vectors of Vectors Representing Objects with Two Dimensions.
Controlling Function Behavior Sequence, Selection and Repetition.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
File I/O ifstreams and ofstreams. Some istream Operations istream function Description cin >> ch; Extract next non-whitespace character from cin and store.
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
Arrays and Vectors Sequential (One-Dimensional) Containers Chapter 12 1.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
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.
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.
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
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.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
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++. 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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 14: Sequential Access Files
ifstreams and ofstreams
Vectors of Vectors Representing Objects with Two Dimensions.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
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.
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
solve the following problem...
Data Streams.
Lecture 5A File processing Richard Gesick.
Basic File I/O and Stream Objects
files Dr. Bhargavi Goswami Department of Computer Science
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O in C++ I.
Vectors of Vectors Representing Objects with Two Dimensions.
CPS120: Introduction to Computer Science
Reading from and Writing to Files
Reading Data From and Writing Data To Text Files
ifstreams and ofstreams
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2

Introduction Up to now we have been: getting input from the keyboard via cin sending output to the screen via cout and cerr We now see how to: get input from a text file send output to a text file 2

Problem 3 data1.txt Project 9 Using OCD, design and implement a program that computes the average of a sequence of numbers stored in a file.

Preliminary Analysis 4 Using #include, a program can read from the keyboard via the istream named cin, but this is of little benefit when the data we need is in a file. What we need is a way to somehow establish an _________-like connection between our __________ and the _________________, such that we can then read from the file via that connection. Program data1.txt

Behavior Our program should display its purpose and then display a prompt for the name of the input file, which it should then read. Our program should open a connection from itself to that input file. It should then read the numbers from the input file via the connection, and compute their sum and count. It should then close the connection, and compute and display the average of the numbers Program data1.txt

Objects Description Type Kind Name purpose, prompt string constant (none) file name string varying inFileName connection ??varying fin a number double varying number sum double varying sum count double varying count average double varying average 6

Operations Description Library? Name display a string string<< read a string stringgetline() open a connection ???? to a file read numbers via ???? connection sum & count numbers built-in +=, ++, loop close connection ???? compute average built-in / display average iostream<< 7 or >> if no blanks in filenames

Algorithm 8 0. Display purpose of program, and prompt for input file name. 1. Read name of input file from cin into inFileName. 2. Open connection named fin to file named in inFileName. 3. Initialize sum, count to zero. 4. Loop: a. Try to read a value from fin into number; b. If no values were left, terminate repetition. c. Add number to sum. d. Increment count. End loop. 5. Close fin. 6. If count > 0 a. Compute average = sum / count. b. Display count and average with appropriate labels Else display error message.

Implementation in C++ To establish connections to an file, the ___________ library provides file-stream types : the ifstream class for input streams the ofstream class for output streams The programmer uses these types to declare file-stream objects; for example, _______________________ 9

To connect such fstream objects to files: Method 1: Use the open() function member in these classes: ifstream fin; __________________________________; ofstream fout; __________________________________; Method 2: Use initializing declarations : ifstream fin("name of file"); ofstream fout("name of file"); 10

Disadvantage of "hard-wiring" actual file) into a program: the program must be modified when using a different file. A better alternative: Have user enter the file name and store it in a string variable; for example, string inFileName; cout << "Enter name of file: "; getline(cin, inFileName); When attaching an fstream to this file, we must use string 's _____________ (or c_str() ) function member to extract the actual file name stored in the string object; for example, ifstream _____________________________; Similar declarations are used for output streams. 11 or use >> if no blanks in filenames

How to read from / write to a file connected to an fstream: Input: Use ____ just like from an ____________ ; fin >> var; Output: Use ____ just like from an ____________ ; fout << expr; 12 or getline() Note: For output, we can use any of the format manipulators such as endl, fixed, setprecison(), and setw() ifstream inherits everything from istream ofstream " " " ostream

13 When we are finished with a file — both for input and output files — we should break the connection between it and the program. Although this will happen automatically when the connection reaches the end of its scope, it is a good idea to do this explicitly with the fstream's function member ____________; for example, fin.close(); fout.close();

Objects Description Type Kind Name purpose, prompt string constant (none) file name string varying inFileName connection ifstream varying fin a number double varying number sum double varying sum count double varying count average double varying average 14

Operations Description Library Name display a string string<< read a string stringgetline() open a connection fstream declaration to a file of fstream (or open() ) read numbers via fstream >> connection sum & count numbers built-in +=, ++, loop close connection fstream close() compute average built-in / display average iostream<< or >> 15

Two More Problems: 1.Opening a file for input can fail. Solution: Use the ifstream function member _______________. ifstream fin(infileName.data()); assert(_______________________); 16 2.How do we know when we all the data in the file has been read? The ifstream function member _________ returns true if an _________________ read found no data remaining in the file. Must try to read and fail In Visual C++: create it in Visual C++ and "Add to" project. Must be in same folder as program.cpp and the.vcproj

Coding 17 /* Program to read data values from a text file, count them, and find their average. Input(keyboard): Name of the file Input(file): doubles Output(screen): Prompts, average of the data value with labels, or file-empty message */ #include // cin, cout,... ______________________________ // ifstream, ofstream,... #include // string #include // assert() using namespace std; int main() { cout << "\nTo average the numbers in an input file," << "\nenter the name of the file: "; string inFileName; getline(cin, inFileName; Note

________________________________; // open the connection 18 OR: ifstream fin; fin.open( infileName.data() ); ________________________________; // verify it opened double number, sum = 0.0; // variables for int count = 0; // computing average for (;;) // input loop { _____________________; // read number if (_______________) break; // if none were left, quit sum += number; // add it to sum count++; // bump up count } // end loop _______________________; // close fstream

19 if (count > 0) { double average = sum / count; cout << "\nThe average of the values in " << inFileName << " is " << average << endl; } else cout << "\n*** No values found in file " << inFileName << endl; }

Testing 20 Create text files in Visual C++ To test our program, we use a text editor and create some easy-to-check input files such as If we name this particular file test1.txt and enter its name when prompted by our program, the average value produced should be 25.

21 Continue testing using other input files, trying to find places where our program breaks down. And it is is important to try various kinds of files — ascending order, descending order, random order, files with 1 element, empty files... — because programs that work for some of these may fail for others. To average the numbers in an input file, enter the name of the file: test1.txt The average of the values in test1.txt is 25 E.G., Project 9

Once we are confident that our program is correct, we can execute it with the data file from our problem: 22 To average the numbers in an input file, enter the name of the file: data1.txt The average of the values in test1.txt is 80.1 data1.txt

Key Things to Remember The fstream library defines two classes: ifstream, for creating connections between programs and input files; and ofstream, for creating connections between programs and output files. Both ifstream and ofstream objects are created in a similar fashion. 23

If inFileName contains the name of an input file, and outFileName contains the name of an output file, then the statements ifstream fin(inFileName.data()); ofstream fout(outFileName.data()); define fin and fout as connections to them. Note that the string function member data() (or c_str() ) must be used to retrieve the actual char- acters of the file’s name from the file stream object. 24 or: ifstream fin; fin.open(inFileName.data()); or: ofstream fout; fout.open(outFileName.data());

If a program tries to open an ifstream to a file that doesn’t exist, the open is said to fail. To check whether or not an ifstream is open, the ifstream class provides the is_open() function member, which returns true if the ifstream was successfully opened, and false if it not. Whether or not a file opened correctly should always be verified using is_open(). 25 assert(fin.is_open()); or assert(!fin.fail()); or use an if

Important Notes About Output Files: If a program tries to open an ofstream to a file that doesn’t exist or that can't be found, the open operation creates a new __________________ for output. If a program tries to open an ofstream to a file that does exist, the open operation (by default) ____________ that file of its contents, creating a clean file for output. 26 To open an existing file for output without emptying it, the value ios::app can be given as a second argument: ofstream fout(outFileName.data(), ios::app); Output will be appended to whatever is already in the file. _____________it! Be careful!

For rare occasions where a file is needed for both input and output, the fstream class is provided: fstream fInOut(ioFileName.data(), ios::in | ios::out); This statement defines an fstream named fInOut to a file, from which data can be read, and to which data can be written. The bitwise OR operator ( | ) is used to combine file open-modes in this manner. 27

Summary 28  #include to use file streams.  To establish a connection to a file whose name is given by a string variable fileName : For input: ifstream fin(fileName.data()); or: ifstream fin; fin.open(fileName.data()) For output: ofstream fout(fileName.data()); or: ofstream fout; fout.open(fileName.data()) fileName Note: data() (or c_str() ) must be used to retrieve the actual characters of the file’s name from fileName.

 To check whether or not an ifstream is open: assert(fin.is_open()); or assert(!fin.fail()); (or use an if statement) 29  Once an ifstream (or ofstream) has been opened, it can be read from (or written to) using the usual input (or output) operations: input: >>, get(), getline(),... output: <<, put(),... In general, anything that can be done to an istream (or ostream) can be done to an ifstream (or ofstream).

 The eof() function member provides a convenient way to build input loops: for (;;) { fin >> someValue; if (fin.eof()) break; //... process someValue }  Remember that opening a file for output erases any contents in the file. 30

 Once we are done using an ifstream (or ofstream), it should be closed using the close() function member: fin.close(); fout.close(); Most systems limit the number of files a program can have open simultaneously, so it is a good practice to close a stream when finished with it.  A closed file can be (re)opened using open() : fin.open(inFileName.data()); fout.open(outFileName.data()); 31