Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard.

Similar presentations


Presentation on theme: "Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard."— Presentation transcript:

1 Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard (cin) and the output stream has defaulted to the console window (cout).

2 Input stream can correspond to a text file. Sample lines of code: ifstream indata indata.open(“file name”) or indata.open(c-style string containing a file name)

3 indata >> variable reads up to white space This could be a tab character, a blank space, or ‘\n’ the end-of-line character If the variable is numeric, it will read up to the first non-digit The character it reads up to is still in the stream!! Useful for reading numerics or multiple strings on a line if they’re delimited by spaces.

4 getline(indata, s) s is a string object and reads up to ‘\n’ and discards the ‘\n’ Useful for reading strings that contain spaces. Typical when the string occupies one whole line of the stream

5 See demo programs. If you follow the instructions I gave at the beginning of the semester for creating a project, the data files are in the project folder. See example program from p. 378.

6 getting and ungetting: ifstream indata indata.get(char variable) will read a character indata.unget(char variable) will put it back See example on page 377 Not common

7 Stream Manipulators Allows formatting of displays. Some of the demo programs used this. See page 383 for specific manipulators.

8 ifstream is a stream for an input file ofstream is a stream for an output file fstream is for reading and writing to a file. Each of these is an istream or an ostream because of inheritance. Note the class hierarchy on streams (p. 380).

9 Advantage of streams and the inheritance hierarchy Can use the same code for different input sources or output destinations. Run the program in notes (from page 381) and test the following cases: A file containing data. A nonexistent file. An empty file. With data entered from the keyboard.

10 The istream class is a base class for other derived classes (inheritance) cin is an istream class. So is an object declared as type ifstream. This is what allows the read_data method from this sample program to be invoked with either cin or infile. Similar statements exist for ostream and ofstream classes.

11 Note the use of istream and ostream classes in the method signatures of demos 3 and 4. This allows input and output to come from (or go to) unspecified sources and destinations. Can write classes independent of data sources and output destinations.

12 Much better reuse. DO NOT have to rewrite stuff just because data sources change. This means you can also replace a console front end with a gui front end. But need string stream classes first.

13 String Streams: istringstream and ostringstream allows you to “read from” and “write to” a string. Though this might seem a bit strange it is incredibly valuable. Note the examples on p. 385-6. Note also the readtime.cpp program on p. 386.

14 For example an object that writes its own data can be given cout, an ofstream object, or an ostringstream object which can be put into a gui text box. This IS USEFUL for reuse and allows independence of the front end the user interacts with. Similar statements can be made for input.

15 demo05 shows how the Bank class from demo04 or demo06 can also be used with a gui interface as opposed to a console based one. Works because istringstream isa istream and ostringstream isa ostream. You will have to copy the bank class and header file from demo04 or demo06 to this project file along with the test harness.

16 Command line arguments (p. 389) All arguments are strings. In VC++.NET do the following to define the parameters: Project -> projectname properties. In the left pane, select debugging. Look for the command line Arguments entry and enter your data values. Example: encryption program of p. 390- 2.

17 Note the random fact on p. 392 covering RSA and PGP.

18 Random Access files (section 9.6) You can skip this section


Download ppt "Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard."

Similar presentations


Ads by Google