The read(), write() and gcount() member functions used to read from standard input, write to standard output and counting the number of characters

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: To read from standard input, write to standard output and counting the number of characters using read(), write() and gcount() C++ member functions

To show: How to use the C++ read(), write() and gcount() member functions to read from standard input, write to standard output and counting the number of characters

 

// using read(), write() and gcount() methods

#include <iostream>

using namespace std;

 

const int SIZE = 100;

 

void main(void)

{

char buffer[SIZE];

 

cout<<"Enter a line of text:"<<endl;

cin.read(buffer,45);

cout<<"The line of text entered was: "<<endl;

// the gcount() member function returns the number of unformatted characters last extracted

cout.write(buffer, cin.gcount());

cout<<endl;

}

 

Output example:

 

Enter a line of text:

This is a line of test text from standard input

The line of text entered was:

This is a line of test text from standard input

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index