Title: st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cin error for large file
Tags: c++;c++11
Question: I was benchmarking some I/O code with large input( 1Mb text file of integer, separated by tab, spaces or endline) then the normal cin method 

```int temp;
cin &gt;&gt; temp;
while(temp!=0){ cin &gt;&gt; temp;}
```

Got into an infinite loop with temp value at 15
, there is no such long sequences in the input file however

The cooked up integer parsing method, with fread however did just fine
with a clock time of around 0.02ms

```void readAhead(size_t amount){
  size_t remaining = stdinDataEnd - stdinPos;
  if (remaining < amount){
    memmove(stdinBuffer, stdinPos, remaining);

    size_t sz = fread(stdinBuffer + remaining, 1, sizeof(stdinBuffer) - remaining, stdin);

    stdinPos = stdinBuffer;

    stdinDataEnd = stdinBuffer + remaining + sz;

    if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)){
      *stdinDataEnd = 0;
    }
  }
}

int readInt(){
  readAhead(16);

  int x = 0;
  bool neg = false;
  // Skipp whitespace manually
  while(*stdinPos == ' ' || *stdinPos == '\n' || *stdinPos == '\t'){
    ++stdinPos;
  }

  if (*stdinPos == '-') {
    ++stdinPos;
    neg = true;
  }

  while (*stdinPos &gt;= '0' &amp;&amp; *stdinPos <= '9') {
    x *= 10;
    x += *stdinPos - '0';
    ++stdinPos;
  }

  return neg ? -x : x;
}
```

Any direction on how might cin get stuck ?

Comment: Thanks ive just realised that.

Comment: It's very simple. If the input does not contain an integer value, the formatted input extraction fails, and the input stream enters error state, which results in all subsequent input operation failing, until the error state gets cleared.

Comment: Fatal logic error: You never check the result of the input operation.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:09/27/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><c++11>', 'Title': 'std::cin error for large file', 'ViewCount': 54, 'id': 46446084, 'user_AboutMe': None, 'user_AccountId': 5371897.0, 'user_DisplayName': 'dxbc', 'user_Reputation': 1.0, 'user_Views': 1.0, 'user_id': 8682388.0}}