fork(2) 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. std::ios_base::sync_with_stdio(false); // de-link C++ streams from their C counterparts
  35.  
  36. for (int i = 0; i < 3; i++)
  37. read();
  38. }
Success #stdin #stdout 0s 16064KB
stdin
1234567890
stdout
cin.readsome() successfully read 3 characters: 123
next char: 4
cin.readsome() successfully read 3 characters: 567
next char: 8
cin.readsome() successfully read 3 characters: 90

next char: �