Course List http://www.c-jump.com/bcc/

C++ Stream I/O


  1. Stream Input/Output
  2. Stream I/O Applications
  3. Stream Output Concept
  4. Stream Input Concept
  5. Using C++ Objects
  6. Standard Output Stream
  7. Standard Output Stream, Cont.
  8. Formatted Output
  9. Standard Input Stream
  10. Standard Input Stream, Cont.
  11. Formatted Input
  12. Unformatted I/O Example
  13. What is EOF?
  14. Unformatted I/O Summary
  15. Formatted I/O
  16. Floating-point Precision
  17. Floating-point Manipulators
  18. Floating-point Example
  19. C++ String
  20. Line-based Input, std::getline
  21. String Stream
  22. Low-level I/O using String Stream
  23. Field Width
  24. Fill Character
  25. Field Alignment
  26. Alignment of Numeric Fields
  27. Scientific Notation
  28. Normalized Scientific Notation
  29. More Manipulator Examples
  30. Manipulators for Integer Data Types
  31. cin interpretation of 0 and 0x prefixes
  32. Manipulators for Floating-point Data Types
  33. C++ Standard Library Objects
  34. Stream Functionality

1. Stream Input/Output



2. Stream I/O Applications



3. Stream Output Concept


  •   hardware monitor

  • 
        std::cout << "HELLO";
        std::cout << '\n';
        std::cout << 123;
    
    
H E L L O \n 1 2 3 ... ...

4. Stream Input Concept


4 5 6 \n 7 8 9 \n ... ...
  •  

      hardware keyboard

  •  

     

    
        int one;
        int two;
        std::cin >> one;
        std::cin >> two;
    
    

5. Using C++ Objects



6. Standard Output Stream



7. Standard Output Stream, Cont.



8. Formatted Output

  • C++ data types have OS- and hardware-specific internal representations...

    • ...If we display a numeric value using formatted output, its value is properly converted to a corresponding sequence of characters.

    • ...If we display a bool, we could convert its value to words "true" or "false".

  • Formatted Output handles value-to-text conversions for us:

     

    7 8 9 ; 0 . 5 ; t r u e

  • For example,

    
    #include <iomanip>
    #include <iostream>
    using std::cout;
    using std::boolalpha;
    
    int main( )
    {
         int i = 789;
         double d = 0.5;
         bool b = true;
    
         cout << i;
         cout << ';';
         cout << d;
         cout << ';';
         cout << boolalpha << b;
         return 0;
    }
    /* Program output:
    789;0.5;true
    */
    
    

9. Standard Input Stream



10. Standard Input Stream, Cont.



11. Formatted Input



12. Unformatted I/O Example



13. What is EOF?



14. Unformatted I/O Summary

  • Unformatted is typically viewed as

    • low-level I/O

    • highest efficiency

    • individual character processing

  • Input normally finishes at end-of-file marker, EOF

  • Input proceeds regardless of multiple lines of text

  • Input has no automatic whitespace recognition

  • 
    // Unformatted output
    std::ostream std::cout;     // predefined
    
    cout.put( c );              // single byte
    
    cout.write( array, count ); // array of bytes
    
    
    
    // Unformatted input
    std::istream std::cin;      // predefined
    
    c = cin.get( );             // single byte
    
    cin.read( array, count );   // array of bytes
    
    

15. Formatted I/O



16. Floating-point Precision

  • Floating-point values are printed with exact fraction part, as specified by the precision.

  • In default notation format, the precision specifies max number of meaningful digits to display both before and after the decimal point.

  • The default precision is 6 digits.

  • This means that the value is rounded to the best approximation while using 6 digits.

  • If necessary, the default format may change to scientific notation to preserve most accurate representation.

  • Precision can be changed by calling precision( ) member function of the stream to improve data accuracy:

    
    #include <iostream>
    int main()
    {
      const double PI = 3.1415926;
      std::cout.precision( 7 );
      std::cout << PI << '\n';
      return 0;
    }
    /*
    Output: 3.141593
    */
    
    

17. Floating-point Manipulators



18. Floating-point Example



19. C++ String

  • std::string stores and manipulates text data

  • Add

    
        #include <string>
    
    

    to bring std::string type into our program

  • 
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        int qty;
        string units;
    
        // Formatted input of text:
        cout << "Qty and units: ";
        cin >> qty;
        cin >> units;
    
        cout << "You entered: ";
        cout << qty;
        cout << ' ';
        cout << units;
        cout << '\n';
        return 0;
    }
    
    

20. Line-based Input, std::getline



21. String Stream



22. Low-level I/O using String Stream



23. Field Width



24. Fill Character



25. Field Alignment



26. Alignment of Numeric Fields



27. Scientific Notation



28. Normalized Scientific Notation



29. More Manipulator Examples



30. Manipulators for Integer Data Types



31. cin interpretation of 0 and 0x prefixes



32. Manipulators for Floating-point Data Types



33. C++ Standard Library Objects



34. Stream Functionality