150 likes | 293 Views
CSE 143. Stream I/O [Appendix C]. 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. Stream I/O.
E N D
CSE 143 Stream I/O [Appendix C]
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
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
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)
Streams in Pictures A stream is just a sequence of characters: someIstreamInstance Input cursor S m y t h e , J . 9 7 0 1 4 3 6 4 input via >> operator C++ Program output via << operator someOStreamInstance Output cursor W e l c o m e t o
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
Stream Output Operation For output streams, << is the “put to” or "insertion" operator #include <iostream.h> … int count = 23; cout << “Hello, World!” << endl; // endl equivalent to “\n” cout << “The count is ” << count << endl;
Stream Input Operation For input streams, >> is the “get from” or "extraction" operator #include <iostream.h> … 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
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
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.
File Stream Example #include <iostream.h> #include <fstream.h> 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(); }
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??
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 • }
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
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;