fork(1) download
  1. /// read() + ignore()
  2.  
  3. #include <iostream> /// cin, cout
  4.  
  5. using namespace std;
  6.  
  7.  
  8. void read()
  9. {
  10. char buf[80] {};
  11.  
  12. cin.readsome(buf, 3);
  13.  
  14. if (cin)
  15. {
  16. cout << "cin.readsome() successfully read "
  17. << cin.gcount() << " characters: ";
  18.  
  19. cout << buf << endl;
  20.  
  21. cout << "next char: " << (char) cin.peek()
  22. << endl;
  23.  
  24. /// discard the character
  25. cin.ignore();
  26. }
  27. else
  28. cout << "cin.readsome() was unsuccessful" << endl;
  29. }
  30.  
  31.  
  32. int main()
  33. {
  34. for (int i = 0; i < 3; i++)
  35. read();
  36. }
Success #stdin #stdout 0s 15240KB
stdin
1234567890
stdout
cin.readsome() successfully read 0 characters: 
next char: 1
cin.readsome() successfully read 0 characters: 
next char: 2
cin.readsome() successfully read 0 characters: 
next char: 3