Professional Documents
Culture Documents
Instant Download PDF C++ For Engineers and Scientists 4th Edition Bronson Solutions Manual Full Chapter
Instant Download PDF C++ For Engineers and Scientists 4th Edition Bronson Solutions Manual Full Chapter
https://testbankfan.com/product/c-for-engineers-and-
scientists-4th-edition-bronson-test-bank/
https://testbankfan.com/product/statistics-for-engineers-and-
scientists-4th-edition-william-navidi-solutions-manual/
https://testbankfan.com/product/modern-physics-for-scientists-
and-engineers-4th-edition-thornton-solutions-manual/
https://testbankfan.com/product/probability-and-statistics-for-
engineers-and-scientists-for-engineers-9th-edition-johnson-
solutions-manual/
Physics For Scientists And Engineers 3rd Edition
Fishbane Solutions Manual
https://testbankfan.com/product/physics-for-scientists-and-
engineers-3rd-edition-fishbane-solutions-manual/
https://testbankfan.com/product/physics-for-scientists-and-
engineers-3rd-edition-knight-solutions-manual/
https://testbankfan.com/product/physics-for-scientists-and-
engineers-9th-edition-serway-solutions-manual/
https://testbankfan.com/product/calculus-for-scientists-and-
engineers-1st-edition-briggs-solutions-manual/
https://testbankfan.com/product/probability-and-statistics-for-
engineers-and-scientists-9th-edition-walpole-solutions-manual/
C++ for Engineers and Scientists, Fourth Edition 8-1
Chapter 8
I/O Streams and Data Files
At a Glance
• Objectives
• Teaching Tips
• Quick Quizzes
• Additional Projects
• Additional Resources
• Key Terms
C++ for Engineers and Scientists, Fourth Edition 8-2
Lecture Notes
Overview
The data for the programs your students have used so far has been assigned internally in the
programs or entered by the user during program execution. As such, data used in these programs
is stored in the computer’s main memory and ceases to exist after the program using it has
finished executing. This type of data entry is fine for small amounts of data. However, your
students can imagine a company having to pay someone to type in the names and addresses of
hundreds or thousands of customers every month when bills are prepared and sent.
In this chapter your students will learn that storing large amounts of data outside a program on a
convenient storage medium is more sensible. They will discover that data stored together under a
common name on a storage medium other than the computer’s main memory is called a data file.
Typically, data files are stored on disks, USB drives, or CD/DVDs. Besides providing permanent
storage for data, data files can be shared between programs, so the data one program outputs can
be input in another program. In this chapter, your students learn how data files are created and
maintained in C++.
Objectives
In this chapter, students will learn about:
• I/O file stream objects and functions
• Reading and writing character-based files
• Random file access
• File streams as function arguments
• A case study about a pollen count file update
• The iostream class library
• Common programming errors
Teaching Tips
8.1 I/O File Stream Objects and Functions
1. Introduce the general concept that to store and retrieve data outside a C++ program two
things are required: a file and a file stream object.
Files
1. Introduce the concept of a file as a collection of data stored together under a common and
unique name on an external medium.
C++ for Engineers and Scientists, Fourth Edition 8-3
2. Review the characteristics of external filenames as they differ between operating systems
and over time.
Teaching Students may find the difference between character and binary file types
Tip confusing since in fact they are both comprised of sets of binary values.
3. Introduce the two basic file types: text or character-based files, and binary-based files.
1. Introduce the concept of a file stream, a one-way transmission path used to connect a
program to a file stored on a physical device.
2. Discuss the difference between input file streams and output file streams.
3. Review the important functions in the ifstream and ofstream objects, and the
related concepts of opening and closing a file.
Teaching Closing a file can be discussed in the larger context of resource management and
Tip ensuring that all resources requested from the operating system are returned.
1. Introduce the concept of the core file stream functions, including opening a file and
closing a file.
Quick Quiz 1
1. What term describes a collection of data stored under a common name?
Answer: a file
2. True or False: The two basic types of files are character-based and binary-based files.
Answer: True
C++ for Engineers and Scientists, Fourth Edition 8-4
3. Make sure that students understand how to specify a file’s location on their computer:
path + filename.
1. Introduce the concept of a logical file as a stream that connects a program to a set of
logically related data.
2. Discuss the concept of a physical file as a stream that connects a program to a hardware
device.
3. Introduce the standard input and standard output files, cin and cout.
4. Highlight the set of devices that are automatically connected when the iostream
header is included in a program.
C++ for Engineers and Scientists, Fourth Edition 8-5
Quick Quiz 2
1. True or False: The actual storage of characters in a character-based file depends on the
character codes the computer uses.
Answer: True
2. What term describes a stream that connects a file of logically related data, such as a data
file, to a program?
Answer: logical file object
3. True or False: There is a big difference between reading data from the keyboard and
reading data from a character-based file.
Answer: False
2. Highlight the difference between random access and sequential file access, touching on
the file position marker functions.
3. Introduce the concept of a file offset as a character’s zero-based position from the start of
a file.
One source of errors in a program that makes use of random file access is to
Teaching
miscalculate file offsets as one-based values. Highlighting the zero-based nature
Tip
of an offset may be very helpful to your students.
Quick Quiz 3
1. True or False: The two types of file access are called sequential and arbitrary.
Answer: False
2. True or False: Using random file access functions, any character in the opened file can be
read without having to sequentially read all characters stored ahead of it.
Answer: True
C++ for Engineers and Scientists, Fourth Edition 8-6
3. True or False: The seek() and tell() family of functions mark positions in a random
access file.
Answer: True
Quick Quiz 4
1. True or False: File stream arguments must never be passed by reference.
Answer: False
1. Review the general functionality provided by the iostream class library, including
encapsulation of the details of reading a byte-stream.
2. You might point out that buffers are used extensively in I/O operations, and that they help
compensate for different data transmission rates.
Your students may find it helpful to view the concept of a device driver in the
Teaching
context of the file as an abstraction permitting access to a large number of
Tip
devices and to data in a large number of formats.
1. Introduce the two primary base classes associated with the iostream: streambuf
and ios.
2. Highlight the set of classes that derive from the ios base class.
In-Memory Formatting
Quick Quiz 5
1. True or False: The iostream class library consists of two primary base classes,
streambuf and ios.
Answer: True
2. Name the object type that is typically used to “assemble” a string from smaller pieces
until a complete line of characters is ready to be written.
Answer: strstream
Quick Quiz 6
1. True or False: Opening a file before attempting to access it is required.
Answer: True
2. True or False: The file’s external name can be used interchangeably with the stream
object name when programming to access the file.
Answer: False
C++ for Engineers and Scientists, Fourth Edition 8-8
3. When required to make existing data available to a program, which stream object
should be used?
Answer: iostream
2. Investigate scenarios where random access is the more appropriate mechanism for file
input.
Additional Projects
1. Have the students write a small C++ program that uses sequential file access to retrieve
an arbitrary line in a text file with fixed length lines whose name and line number (base
1) is specified on the command line. Then have the students convert the program to use
random access to perform the same functions.
Additional Resources
1. Tutorial on iostream use:
www.devarticles.com/c/a/Cplusplus/Iostream-Library-and-Basic-IO-in-Cplusplus/
Key Terms
Binary-based files: Files that use the same codes as the C++ compiler uses to store data
Character-based files: Files where the data is stored using a character code set such as
ASCII
Closing a file: Closing a connection between stream objects
Device driver: A section of operating system code that accesses a hardware device and
handles data transfer between the device and the computer’s memory
C++ for Engineers and Scientists, Fourth Edition 8-9
External name: The unique name by which a file is known to the operating system
File: A collection of data stored together under a common name
File access: The process of retrieving data from a file
File organization: The way in which data is stored in a file
Logical file object: A stream that connects a file of logically related data to a program
Offset: A character’s position in a file
Opening a file: Connecting a stream object name to an external filename
Output mode: The state of a file connected to an output file stream where the stream is
available for writing
Physical file object: A stream that connects a hardware device to a program
Random access: The type of file access in which any character in the opened file can be
read without having to sequentially read all characters stored ahead of it first
Sequential access: The type of file access that involves reading characters one after
another from an open input file stream
Sequential organization: Characters in a file are stored in a sequential manner
Standard output file: The standard object, cout, that is usually automatically created
and available for data entry
Text files: Files where the data is stored using a character code set such as ASCII
Another random document with
no related content on Scribd:
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must, at
no additional cost, fee or expense to the user, provide a copy, a
means of exporting a copy, or a means of obtaining a copy upon
request, of the work in its original “Plain Vanilla ASCII” or other
form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using
the method you already use to calculate your applicable
taxes. The fee is owed to the owner of the Project
Gutenberg™ trademark, but he has agreed to donate
royalties under this paragraph to the Project Gutenberg
Literary Archive Foundation. Royalty payments must be paid
within 60 days following each date on which you prepare (or
are legally required to prepare) your periodic tax returns.
Royalty payments should be clearly marked as such and
sent to the Project Gutenberg Literary Archive Foundation at
the address specified in Section 4, “Information about
donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.