Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 C++ Lecture 8 Monday, 25 August 2005

2 I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing

3 IO Stream Library l -- basic I/O l -- formatted I/O l -- file l Take a look of iostream.h (at C:\MinGW\include\c++\)

4 Stream I/O Classes ios istreamostream iostream ifstreamofstream

5 Standard Stream Objects l cin – istream class, “tied to” (connected to) the standard input device (keyboard) l cout – ostream class, “tied to” standard output device l cerr – ostream class, standard error output, unbuffered l clog – ostream class, also to standard error, buffered

6 > Overloaded Operators l cout << A; // type need not specify l [compare with printf(“%d”, A);] l cout << A << B; // cascading l cout << endl; // newline l cout << flush; // forced buffer flush

7 Put and Get Member Functions l cout.put(‘A’); // print a single char l cin.get( ); // get a single char l cin.getline(buffer, SIZE); // read a line of characters l cin.eof(); // test for end-of-file C.f. Fig. 11.12 (old)

8 Unformatted I/O l cout.write(buffer, SIZE) l cin.read(buffer, SIZE) l The memory contents pointed by buffer is read/write. l In formatted I/O, contents are translated into printable ASCII sequence

9 Printing in Other Bases l cout << n; l cout << hex << n; l cout << dec << n; l cout << oct << n; l cout << setbase(10) << n;

10 Format States l setiosflag(iso::S) l Where S can be skipws, left, right, dec, oct, showpoint, uppercase, fixed etc.

11 Write in a File l #include l … l ofstream fileobj(“f.dat”, ios::out); // create output file object l fileobj << data; // output to file l ofstream is derived class from ostream

12 Read in a File l #include l … l ifstream fileobj(“f.dat”, ios::in); // create input file object l fileobj >> data; // read from file l ifstream is derived class of istream C.f. Fig. 14.7

13 Open and Close File l Using scope rule { ofstream myfile(“dat.d”, ios::out); myfile << x; } l Explicit open and close ofstream myfile; myfile.open(“dat.d”, ios::out); myfile << x; myfile.close();

14 Sequential v.s. Random Access of Files l Normally, cin or cout or file stream is used sequentially l Using the stream member functions seekp( ) and write( ), we can do random file access

15 Preprocessing l 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. l #define …

16 #include Preprocessor Directive l #include // standard // location l Or l #include “filename” // user // working directory l A copy of the file is “physically” included

17 #define Directive l #define CURRENT_H l #define PI 3.14159 l #define SQ(x) ((x)*(x)) l Any identifier in #define is replaced by replacement text

18 #define Examples l #define SQ1(x) ((x)*(x)) l #define SQ2(x) x*x l Then B = SQ1(a+1); C = SQ2(a+2); l becomes B = ((a+1)*(a+1)); C = a+2*a+2;

19 Conditional Compilation #if defined(IBM) … do such and such #elif // optional … do … #else // optional … #endif

20 # and ## Operators l # converts text to string l ## concatenates two tokens #define H(x) cout << “Hi,” #x #define C(x,y) x ## y H(JS); z = C(x, 5); l becomes cout << “Hi,” “JS”; z = x5;

21 Tutorial Problems l Will the following program works? (Read a string and output the string) #include int main() { string s; cin >> s; cout > s.size() >> endl; }

22 A Matrix Class l Discuss varies possible ways of implementing a matrix class, discuss the issue of efficiency, memory management, operator overloading, etc.


Download ppt "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."

Similar presentations


Ads by Google