External_Files - More about External File Stream Input and...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
More about… External File Stream Input and Output CS 162: Introduction to Computer Science II Overview Earlier, we learned how to use the iostream library to perform basic input from and output to standard input, standard output, and standard error. Now it is time to extend what we learned and see how it applies when we read from or write to external files. The following sections focus on the basic file stream I/O facilities and touch upon some of the most popular features when dealing with external text and binary files. We begin by understanding how to get started reading and writing external files. We progress then to look at how different data types are handled and when we need to use care. It is important to remember that both Appendix A and B are designed for those just getting started using C++ and I/O. Getting Started with File Steam Input and Output Input and output is performed as a stream of bytes handled through external libraries. This applies to external files in the same way that it applies to standard input and output, except that we use the fstream library instead. This library supplies the operations necessary to convert objects to sequences of characters for output to external files and to convert sequences of characters to objects of various types for input from external files. We can use the fstream library in exactly the same manner as we use the iostream library. The extraction operator, get , getline , read , and ignore operations are available for reading from external files. The insertion operator and put function are available for writing to external files (along with all of the formatting controls). Therefore, how we use external files follows the same rules as when we use standard input and output. The only difference is in the process of attaching our objects to specific external files. External files simply represent a different stream of input and output. The fstream library includes the operations of the iostream library and predefines a set of operations for handling reading and writing of built-in data
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
types when working with external files. The fstream library defines three new data types: ifstream , ofstream , and fstream . The following sections help us get started using the fstream library. Preprocessor Directive C++ provides an enhanced fstream library for reading and writing external files. To use it, we must include the following preprocessor directive: #include <fstream> This is necessary because file I/O is not built into the language. The good news is that when we include the fstream library, we no longer need to also include the iostream library. This is because the fstream library "is an" iostream library in addition to being able to work with files. By including this library, we gain access to all of the necessary operations to perform input and output for standard in, standard output, standard error as well as for external files. This includes the ability to work with wide character streams. Then, at link
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 14

External_Files - More about External File Stream Input and...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online