c++ lecture 8 monday, 25 august 2005. i/o, file, and preprocessing l an in-depth review of stream...
TRANSCRIPT
![Page 1: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/1.jpg)
C++
Lecture 8Monday, 25 August 2005
![Page 2: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/2.jpg)
I/O, File, and Preprocessing
An in-depth review of stream input/output
File handling in C++
C++ preprocessing
![Page 3: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/3.jpg)
IO Stream Library
<iostream> -- basic I/O <iomanip> -- formatted I/O <fstream> -- file
Take a look of iostream.h (at C:\MinGW\include\c++\)
![Page 4: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/4.jpg)
Stream I/O Classes
ios
istream ostream
iostreamifstream ofstream
![Page 5: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/5.jpg)
Standard Stream Objects
cin – istream class, “tied to” (connected to) the standard input device (keyboard)
cout – ostream class, “tied to” standard output device
cerr – ostream class, standard error output, unbuffered
clog – ostream class, also to standard error, buffered
![Page 6: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/6.jpg)
<< and >> Overloaded Operators
cout << A; // type need not specify [compare with printf(“%d”, A);]
cout << A << B; // cascading cout << endl; // newline cout << flush; // forced buffer flush
![Page 7: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/7.jpg)
Put and Get Member Functions
cout.put(‘A’); // print a single char cin.get( ); // get a single char cin.getline(buffer, SIZE); // read a line of characters cin.eof(); // test for end-of-file
C.f. Fig. 11.12 (old)
![Page 8: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/8.jpg)
Unformatted I/O
cout.write(buffer, SIZE) cin.read(buffer, SIZE)
The memory contents pointed by buffer is read/write.
In formatted I/O, contents are translated into printable ASCII sequence
![Page 9: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/9.jpg)
Printing in Other Bases
cout << n; cout << hex << n; cout << dec << n; cout << oct << n; cout << setbase(10) << n;
![Page 10: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/10.jpg)
Format States
setiosflag(iso::S) Where S can be skipws, left, right,
dec, oct, showpoint, uppercase, fixed etc.
![Page 11: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/11.jpg)
Write in a File
#include <iostream> #include <fstream> … ofstream fileobj(“f.dat”, ios::out); //
create output file object
fileobj << data; // output to file
ofstream is derived class from ostream
![Page 12: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/12.jpg)
Read in a File
#include <iostream> #include <fstream> … ifstream fileobj(“f.dat”, ios::in); //
create input file object
fileobj >> data; // read from file
ifstream is derived class of istream
C.f. Fig. 14.7
![Page 13: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/13.jpg)
Open and Close File
Using scope rule
{ ofstream
myfile(“dat.d”,ios::out);
myfile << x;}
Explicit open and close
ofstream myfile;myfile.open(“dat.d”,
ios::out);myfile << x;myfile.close();
![Page 14: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/14.jpg)
Sequential v.s. Random Access of Files
Normally, cin or cout or file stream is used sequentially
Using the stream member functions seekp( ) and write( ), we can do random file access
![Page 15: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/15.jpg)
Preprocessing
Before sending the source file to the compiler proper, C/C++ passes the source code to preprocessor (e.g., cpp or cc –E on Unix) to process text related to preprocessing derivatives, e.g.
#define …
![Page 16: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/16.jpg)
#include Preprocessor Directive
#include <filename> // standard // location Or #include “filename” // user // working directory A copy of the file is “physically”
included
![Page 17: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/17.jpg)
#define Directive
#define CURRENT_H #define PI 3.14159
#define SQ(x) ((x)*(x))
Any identifier in #define is replaced by replacement text
![Page 18: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/18.jpg)
#define Examples
#define SQ1(x) ((x)*(x)) #define SQ2(x) x*x Then
B = SQ1(a+1); C = SQ2(a+2); becomes
B = ((a+1)*(a+1));C = a+2*a+2;
![Page 19: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/19.jpg)
Conditional Compilation
#if defined(IBM) … do such and such#elif // optional … do …#else // optional …#endif
![Page 20: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/20.jpg)
# and ## Operators
# converts text to string ## concatenates two tokens#define H(x) cout << “Hi,” #x#define C(x,y) x ## yH(JS); z = C(x, 5); becomescout << “Hi,” “JS”; z = x5;
![Page 21: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/21.jpg)
Tutorial Problems
Will the following program works? (Read a string and output the string)
#include <string>int main(){ string s; cin >> s; cout << “length of the string is ” >>
s.size() >> endl;}
![Page 22: C++ Lecture 8 Monday, 25 August 2005. I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649ef95503460f94c0b21f/html5/thumbnails/22.jpg)
A Matrix Class
Discuss varies possible ways of implementing a matrix class, discuss the issue of efficiency, memory management, operator overloading, etc.