Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENERGY 211 / CME 211 Lecture 9 October 10, 2008.

Similar presentations


Presentation on theme: "ENERGY 211 / CME 211 Lecture 9 October 10, 2008."— Presentation transcript:

1 ENERGY 211 / CME 211 Lecture 9 October 10, 2008

2 Streams To exchange data between memory and I/O devices or storage, use streams cin, cout are examples of streams Some member functions: clear() resets stream to allow read/write bad() returns true if stream in bad state good() returns true if stream usable eof() returns true if no input available istream, ostream have useful subclasses for I/O with various sources

3 Console Streams Declared in <iostream> header
Predefined instances of class ostream write data to the screen cout for standard output cerr for reporting errors clog for writing logging data Predefined instance cin, of class istream, reads from the keyboard

4 File Streams Declared in <fstream> header
Use ofstream object to write to file named at construction ifstream object allows reading from a file named at construction Constructors want a const char *, so use c_str() member of string Available functions/operators: >>, <<, char get(), put(char), getline(istream, string), etc.

5 String Streams Declared in <sstream>
ostringstream object (derived from ostream) allows writing to a string, accessed with str() member function istringstream object (derived from istream) allows reading from a string designated at construction Usual functions/operators available Useful for working with a line of input in memory instead of in another stream

6 Manipulators The following belong to the std namespace, and are used with >>, << operators endl outputs a newline and flushes output buffer setw(int n) sets the field with for the next output to n right, left set justification of output dec, oct, hex set base for reading or writing integers

7 Persistence Data in program's memory goes away when execution ends
To make data persistent, write to file Then, program can read from file to resume working with data >>, << operators allow simple method of making data persistent For more complex objects, requires agreed-on format to map text to object

8 structs A struct is a primitive data structure that allows data of different types to be grouped into a single entity Primarily used in C; in C++, superseded by classes, which offer much more functionality A struct contains fields, or members, which are given any names and types Fields are stored consecutively in memory, in order of declaration

9 structs, cont’d Form of declaration:
struct { type1 name1; type2 name2; … } varname; To access fields, use the . operator: structname.fieldname To access field through pointer to struct, use arrow operator: pointername->fieldname To associate type name with struct: typedef struct { … } typename;

10 Character functions Declared in <cctype> header
Useful for classifying or manipulating characters (and for Project 2!) isalnum(c): true if c is letter or digit isalpha(c): true if c is a letter iscntrl(c): true if c is a control character isdigit(c): true if c is a digit

11 More Character Functions
islower(c): true if c is uppercase ispunct(c): true if c is punctuation isspace(c): true if c is white space (space, tab or newline) isupper(c): true if c is uppercase tolower(c) converts c to lowercase toupper(c) converts c to uppercase

12 Next Time All about pointers! Project 2 Questions


Download ppt "ENERGY 211 / CME 211 Lecture 9 October 10, 2008."

Similar presentations


Ads by Google