Functions Manipulating Files

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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
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.
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
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
ifstreams and ofstreams
Vectors of Vectors Representing Objects with Two Dimensions.
Data File Handling in C++
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.
Lab 7: File Input/Output Graham Northup
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
solve the following problem...
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
File I/O.
Standard Input/Output Streams
Standard Input/Output Streams
Lecture 5A File processing Richard Gesick.
Working with Data Files
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 3: Input/Output
Standard Input/Output Stream
Chapter 3 Input output.
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.
Data File Handling RITIKA SHARMA.
Reading Data From and Writing Data To Text Files
ifstreams and ofstreams
File I/O in C++ I.
Presentation transcript:

Functions Manipulating Files File Operations Functions Manipulating Files

Review 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. 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 open the 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 prompt string local -- file name string in (kbd) fileName connection ifstream out fin 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 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 (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 (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.