24 4/11/98 CSE 143 Stream I/O [Appendix C]
25 4/11/98 Input/Output Concepts Concepts should be review! New syntax, but same fundamental concepts input vs. output, read vs. write file; file name vs. file variable open; close end-of-file conversion from character steam to C/C++ types
26 4/11/98 Stream I/O C++ uses the concept of streams for I/O both for keyboard/monitor and for files C++ can also use old C-style printf, scanf, etc. Mixing the two is not recommended You must use only stream I/O in CSE143
27 4/11/98 What is a Stream? Think of a stream as a sequence of characters istream and ifstream are types of input streams Keyboard ( istream ) File ( ifstream ) ostream and ofstream are types of output streams Screen ( ostream ) File ( ofstream )
28 4/11/98 Streams in Pictures A stream is just a sequence of characters: Smythe, J someIstreamInstance Input cursor Welcome to someOStreamInstance Output cursor C++ Program input via >> operator output via << operator 64
29 4/11/98 Well-Known Streams Global streams defined in iostream.h : cin : standard input stream (usually keyboard) cout : standard output stream (usually screen) cerr : standard error stream Programs can open other streams to/from files and other devices: fstream.h : for input/output with files strstream.h : for treating strings as streams
30 4/11/98 Stream Output Operation For output streams, << is the “put to” or "insertion" operator #include … int count = 23; cout << “Hello, World!” << endl; // endl equivalent to “\n” cout << “The count is ” << count << endl;
31 4/11/98 Stream Input Operation For input streams, >> is the “get from” or "extraction" operator #include … int x, ID; char Name[40]; cin >> x; cin >> Name >> ID; // Can read multiple items on one line // Note: no &’s as needed with scanf
32 4/11/98 How Stream Input Works Input stream characters are interpreted differently, depending on the data type: int ID; char Name[40]; char ch; cin >> ID; // interprets as integer cin >> ch; // reads a char, skipping whitespace cin >> Name; // interprets as character string, // skipping leading whitespace and //stopping at trailing whitespace
33 4/11/98 Other Stream Operations Sometimes want to read from a file without ignoring whitespace (spaces, tabs, newlines) get(c);// reads one character into c Get also returns a value which we can test to see if the operation succeeded. (It would fail, for instance, if the end-of-file was reached) There are several varieties of get() and other I/O operations. See Appendix C and Lippman for details.
34 4/11/98 File Stream Example #include void main() { ifstream inFile("input.txt");// open input ofstream outfile("output.txt");// open output char ch; // should test for successful opening here.. while (inFile.get(ch)) {// while more input outFile.put(ch);// write it to output } inFile.close();// close the files... outFile.close(); }
35 4/11/98 Preview of C++ Classes Streams are instances of C++ classes Class instances are like structs in that they have fields (generally called members). Members can be data or functions (methods) We use the “.” syntax to access members. Eg: inFile.get(ch);// get a character outFile.put(ch);// put a character outFile.close();// close the stream outFile.eof();// end of File??
36 4/11/98 Stream Testing We often want to test the status of a stream, to see if it has any more data. It’s usually cleanest to use the return value of the input function … while (cin.get(ch)) {... } while (cin >> someInt) { // if the get succeeded // do something }
37 4/11/98 Stream Testing (2) It’s also possible to use the eof() member function: if (!someFile.eof()) {... } For keyboard input, special characters such as Ctrl-Z (on most PCs) or Ctrl-D (on UNIX) signal end-of-file
38 4/11/98 Stream Advantages Type Safety No more dangers of mismatched % -types scanf("%s", &someInt); // oops! scanf("%d", someInt); // dooh! Readability Easier to interpret than a sequence of % -types scanf("%d%c%d", &intOne, &oneChar, &intTwo); versus cin >> intOne >> oneChar >> intTwo;