File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and.

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

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.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
File I/O. Stream Objects cin – the standard input stream - an object of the istream class, cout – the standard output stream – an object of the ostream.
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
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.
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.
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.
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.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Vectors of Vectors Representing Objects with Two Dimensions.
Controlling Function Behavior Sequence, Selection and Repetition.
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.
C++ Loop Statements Repetition Revisited. Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
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.
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.
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.
Files and Streams Chapter 9. C++ An Introduction to Computing, 3rd ed. 2 Objectives Use OCD to solve a problem involving files Study C++'s support for.
Chapter 3: Input/Output
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.
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.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring Fall 2016 Set 10: Input/Output Streams 1 Based on slides created.
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.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Learners Support Publications Working with Files.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
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.
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.
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.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
ifstreams and ofstreams
Vectors of Vectors Representing Objects with Two Dimensions.
Functions Manipulating Files
Copyright © 2003 Pearson Education, Inc.
File I/O.
Standard Input/Output Streams
Standard Input/Output Streams
Lecture 5A File processing Richard Gesick.
Programming with Data Files
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Standard Input/Output Stream
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O in C++ I.
File I/O.
Vectors of Vectors Representing Objects with Two Dimensions.
Programming with Data Files
ifstreams and ofstreams
File I/O in C++ I.
Presentation transcript:

File Operations Functions Manipulating Files

Review The fstream library defines two classes: ifstream, for creating connections between programs and input files; andifstream, for creating connections between programs and input files; and ofstream, for creating connections between programs and output files.ofstream, for creating connections between programs and output files. Each can be operated on in much the same way as a normal istream or ostream.

Problem 1 Using OCD, design and implement a function that reads the name of an input file from the user, tries to open it, and returns to the caller an ifstream guaranteed to be open, and guaranteed to be connected to a file specified by the user.

Preliminary Analysis One way to guarantee both conditions: the ifstream is openthe ifstream is open the file was specified by the userthe file was specified by the user is to use a loop that gives the user another chance if they enter an invalid file name...

Behavior Our function should display a prompt for the name of the input file. It should read the name of the input file. It should try to open an ifstream to that file. If the ifstream opens successfully, our function should return that ifstream; otherwise, it should display an error message, “loop back” and give the user another chance.

Objects Description Type Movement Name file name string in (kbd) fileName connection ifstream out fin prompt string local -- error msg string local --

Operations Description Predefined? Library? Name display a string yes string << read a string yes string >> open connection yes fstream -- to a file verify connection yes fstream is_open repeat on failure yes built-in loop return an ifstream yes built-in return

Algorithm 1. Loop: a. Display prompt for input file name. b. Read fileName. c. Open ifstream connection named fin to fileName. d. If fin opened successfully, return fin. e. Display error message End loop.

Discussion We have seen how to open an ifstream to a file, verify that the open succeeded, etc. We can have a function return an ifstream by making its return-type ifstream. For functions that need to receive an ifstream or ofstream, these types can be used to define function parameters.

Discussion (Ct’d) If we wish to avoid declaring fin within the loop, we need a way to open fin at a point different from its declaration. The ifstream (and ofstream) classes provide the function member to open a stream at a point other than its declaration. The ifstream (and ofstream) classes provide the function member open() to open a stream at a point other than its declaration.

Coding /* GetIFStream() *... */ ifstream GetIFStream() { ifstream fin; // declare fin here string fileName; for (;;) { cout << “\nEnter the name of the input file: “; cin >> fileName; fin.open(fileName.data()); // open fin here if (fin.is_open()) return fin; // verify it opened cout << “\n*** Unable to open file \’” << fileName << “\’ for input!\n” << endl; }

Discussion In addition to the file operations we have seen, the iostream (and fstream) libraries provide a variety of additional operations for manipulating streams.

Status Operations To determine the status of a stream, the libraries provide these function members: –good() // returns true iff stream is ok –bad() // returns true iff stream is not ok –fail() // returns true iff last operation failed –eof() // returns true iff last file-read failed

Change-State Operations To change the state of a stream, the libraries provide these function members: –clear() // reset status to good –setstate(b) // set state bit b (one of ios_base::goodbit,ios_base::badbit, ios_base::failbit, or ios_base::eofbit).

Read-Position Operations To manipulate the read-position within an ifstream, the libraries provide these: –tellg() // returns offset of current read-position from beginning of file –seekg(offset, base) // move read-position offset bytes from base offset bytes from base (one of ios_base::beg, ios_base::cur, or ios_base::end)

Write-Position Operations To manipulate the write-position within an ofstream, the libraries provide these: –tellp() // returns offset of current write-position from beginning of file –seekp(offset, base) // move write-position offset bytes from base offset bytes from base (one of ios_base::beg, ios_base::cur, or ios_base::end)

Other Operations To look at the next character in an ifstream without advancing the read-position (i.e., without reading it), the libraries provide: –peek() // returns next char in the stream without reading it To “unread” the last char that was read, the libraries provide: –unget() // unread char most recently read

Another Operation To skip a given number of chars in the stream (or until a particular char is encountered), the libraries provide: –ignore(n, stopChar) // skip past n chars, or until stopChar is encountered

Discussion This is by no means an exhaustive list, but it does give some of the most commonly-used stream function members. See Chapter 21 of “The C++ Programming Language” by Bjarne Stroustrup (Addison- Wesley) for a complete list.

Summary The C++ iostream library provides a rich set of I/O functions that let a programmer: –open and close streams. –read-from/write-to streams. –get/set the state of a stream. –get the read/write position of a stream. –move the read/write position of a stream. –peek at, or unget chars from a stream. –skip over chars in a stream.