SlideServe
Menu
Préférences
© SlideServe · v2026

CSE 143

lynda avatar
15 diapositives · 314 Views · Sep 07, 2014

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.

CSE 143 - slide 1
01 / 15CSE 143
1 / 17

Télécharger la présentation

CSE 143
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. CSE 143 Stream I/O [Appendix C]

  2. 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

  3. 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

  4. 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)

  5. 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

  6. 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

  7. 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;

  8. 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

  9. 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

  10. 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.

  11. 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(); }

  12. 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??

  13. 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 • }

  14. 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

  15. 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;