Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ REVIEW INPUT/OUTPUT (I/O). BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in.

Similar presentations


Presentation on theme: "C++ REVIEW INPUT/OUTPUT (I/O). BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in."— Presentation transcript:

1 C++ REVIEW INPUT/OUTPUT (I/O)

2 BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in this class can be composed or reworked for more complex data structures in “real” code

3 SECOND BRIEF NOTE RAII RAII (Resource Acquisition Is Initialization) Stupid name (admitted by Stroustrup himself) One of the most important C++ concepts! (also admitted by Stroustrup) Simple idea Acquire resources (memory, file pointers, threads) in constructors Release them in destructors Compiler will automatically handle calling destructors (for non pointers…) Important implications in complex code blocks with multiple exits Start of function Acquire resources? Error detected Release resources first Throw will exit function Return 1 Release resources Return 2 Release resources Start of function Acquire resources? Error detected Throw will exit function Return 1Return 2 Without RAII With RAII

4 ON I/O Algorithms always deal with I/O Data has a need to be persistent or transferred between programs Data could be too large to store in RAM Input (Data) Algorithm (Process Data) Output

5 MODERN I/O IN C++ Utilizes “streams” Standard Streams ( ) File Streams ( ) String Streams ( ) All streams are based on same concepts and operations and can be broken down into two basic types Input streams Output streams

6 BASIC STREAM OPERATIONS << – “put to” or “puts” operation Send data to an output stream (note I said “data” and not “string”) cout << “Howdy Aggieland! Home of the ” << 12 << “th man!\n”; >> – “get from” or “gets” operation Read data from an input stream double d; cout << “Enter your favorite rational decimal: ”; cin << d; For user defined types – overload these operators)

7 STANDARD STREAMS System level I/O, i.e., to the console cin – Standard Input cout – Standard Output cerr – Standard output stream for Errors clog – Standard output stream for Logging Examples cout << “Hello” << endl; cerr << “Kudos, you messed up” << endl;

8 FILE STREAMS Read/Write to/from external files Output – ofstream Input – ifstream Example ofstream ofs(“MyFile.txt”); if(ofs) //open was a success ofs << “Hello file streams!” << endl; Side Question File streams come with a function close(), which releases the file pointer. Why am I not calling it?

9 STRING STREAMS Write (convert) data to strings or format strings in nice ways ( ostringstream ) Read data from lines of a file or as tokens from a larger string ( istringstream ) Example double d = 3.14; int i = 5; ostringstream oss; oss << d << “\t” << i;

10 ESCAPE SEQUENCES Special characters in strings like \n – new line \r – carriage return \t – tab spacing \’ – single quote \” – double quote \\ – backslash etc

11 FORMATTING STRINGS Use Format for human readability of data Some of the most common uses setw(int n) – set field width cout << setw(10) << 5; //produces “ 5”; setprecision() – sets format of decimal numbers cout << setprecision(5) << 3.1415926; //produces “3.1416” cout << fixed << 3.1415926; //now produces “3.14159” //(notice set precision 5 is still active) Can set to scientific notation as well Can automatically format time and money


Download ppt "C++ REVIEW INPUT/OUTPUT (I/O). BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in."

Similar presentations


Ads by Google