Use cin.read() and cin.gcount() - C++ File Stream

C++ examples for File Stream:cin

Description

Use cin.read() and cin.gcount()

Demo Code

#include <iostream>

int main(int argc, const char *argv[]) {
    const int SIZE = 80;
    char buffer[SIZE];

    // use function read to input characters into buffer
    std::cout << "Enter a sentence:" << std::endl;
    std::cin.read(buffer, 20);/*  w w  w  . ja v a2 s  .  com*/

    // use functions write and gcount to display buffer characters
    std::cout << std::endl << "The sentence entered was:" << std::endl;
    std::cout.write(buffer, std::cin.gcount());
    std::cout << std::endl;

    return 0;
}

Result


Related Tutorials