1 / 9

what is a stream? what is the difference between an input stream and an output stream?

Questions about File I/O. what is a stream? what is the difference between an input stream and an output stream? what streams do not need definitions? how can a stream be defined? what do the following functions do? open() close() c_str() eof() fail() exit()

veola
Download Presentation

what is a stream? what is the difference between an input stream and an output stream?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Questions about File I/O • what is a stream? • what is the difference between an input stream and an output stream? • what streams do not need definitions? • how can a stream be defined? • what do the following functions do? • open()close() • c_str() eof()fail()exit() • can a stream be passed as a parameter? How?

  2. Structures

  3. Structure Definitions • structures are an aggregate construct – used to refer to a collection of diverse data by one name • example: bank’s certificate of deposit (CD) - has balance (double), rate (double) and months until maturity (int) • structure definition: struct CDAacct{ double balance; int rate; int term; }; • note the semicolon at the end of the structure definition! • elements of the structure are called members or member variables • a structure definition is not executable - good candidate for a header file • members of the same structure must have different names, different structures can contain members with the same name

  4. Preprocessor Directives • Recall that each definition (including structure definitions) can only be encountered once during compilation. • When a structure definition is placed in a header file, careful must be taken so it is not read twice • mechanism - preprocessor directives #define name value note that substitution is textual • problem: #define press 50 int press = 30 • gets substituted as int 50 = 30 => syntax error! #ifdef name - true if name defined, #ifndefname - true if not #endif - completes #if • header file myheader.h containing definitions usually has the following structure: #ifndef MYHEADER_H #define MYHEADER_H // text of the header file goes here #endif

  5. Structure Variables • when a structure like CDAcct is defined, structure variables can be declared as any other variables: CDAcct myacct, youracct; • each structure variable contains three member variables • the programmer can refer to member variables by specifying the structure variable name “dot” member variable name • for example myacct contains: myacct.balance myacct.rate myacct.term • the member variables are of type specified in the structure definition • the member variables can be used as any other variables myacct.balance=1000.00; myacct.rate = youracct.rate+1; cin >> myacct.term;

  6. Assigning Values toStructure Variables • initialization assignment • structure variable can be initialized at definition: CDacct myacct={1000.0, 9.9, 12} • order of initialization corresponds to the order the member variables are listed in the structure definition • if there are more values than variable members - error • if there are less values than variable members - the rest are initialized to 0 • a structure variable can be assigned the value of another structure variable provided they have the same structure type CDacct youracct; youracct=myacct; • However, structure variables cannot be compared directly if (youracct == myacct) // error

  7. Passing Structures as Parameters,Returning Structures • structures can be passed by value and by reference, a function can return a structure: CDaccount setCD(double b, double r, int t){ CDAccount tmp; tmp.balance=b; tmp.rate=r; tmp.term=r; return tmp; }

  8. Complex Structures • a member may be of basic type or of type structure (referred to as substructure): // example structure struct example{ int a; string b; }; // second example structure // whose member is a substructure struct compExample{ int c; example d; }; • if there is a declaration compExample me;how do we access substructure member variables?

  9. Structures and Arrays • a member may be an array struct example{ // defining structure int a; int b[5]; }; example se; // declaring structure variable se.a = 123; se.b[3]=456; // accessing members • an array of structures may be declared struct example{ // defining structure int a; string b; }; example se[4]; // declaring array of structures se[3].a=123; se[3].b=”Hello”; // accessing members how do we access elements of string b? • more complicated constructs (arrays of structures with substructures, etc.) are possible

More Related