Unformatted I/O using read, write and gcount

Unformatted I O using read, write and gcount

Unformatted input/output is performed using the read and write member functions of istream and ostream, respectively. Member function read inputs some number of bytes to a character array in memory; member function write outputs bytes from a character array. These bytes are not formatted in any way. They are input or output as raw bytes. For example, the call

char buffer[] = "HAPPY BIRTHDAY";
cout.write( buffer, 10 );

outputs the first 10 bytes of buffer (including null characters, if any, that would cause output with cout and << to terminate). The call


displays the first 10 characters of the alphabet.

The read member function inputs a designated number of characters into a character array. If fewer than the designated number of characters are read, failbit is set. Section 15.8 shows how to determine whether failbit has been set. Member function gcount reports the number of characters read by the last input operation.

Figure 15.7 demonstrates istream member functions read and gcount and ostream member function write. The program inputs 20 characters (from a longer input sequence) into character array buffer with read (line 15), determines the number of characters input with gcount (line 19) and outputs the characters in buffer with write (line 19).

Figure 15.7. Unformatted I/O using the read, gcount and write member functions.

 1 // Fig. 15.7: Fig15_07.cpp
 2 // Unformatted I/O using read, gcount and write.
 3 #include 
 4 using std::cin;
 5 using std::cout;
 6 using std::endl;
 8 int main()
 9 {
10 const int SIZE = 80;
11 char buffer[ SIZE ]; // create array of 80 characters
13 // use function read to input characters into buffer
14 cout << "Enter a sentence:" << endl;
15 cin.read( buffer, 20 );
17 // use functions write and gcount to display buffer characters
18 cout << endl << "The sentence entered was:" << endl;
19 cout.write( buffer, cin.gcount() );
20 cout << endl;
21 return 0;
22 } // end main
 Enter a sentence:
 Using the read, write, and gcount member functions
 The sentence entered was:
 Using the read, writ

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism


Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger


show all menu

C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net