有什么方法可以区分规范模式下的 EOL 和 EOF 吗?

Is there any way to tell the difference between EOL and EOF in canonical mode?

如果您在规范模式下从终端读取行,有没有办法区分以 EOL (Enter) 和 EOF (Ctrl-D) 终止的行?我的理解是,两者都将 return 从 read().

读取的字节数

对于空行上的 EOL,对于换行符,字节数将为 1。

对于空行上的 EOF,字节数将为 0,对于 'there was no more data'。

查看最后读取的字符(除非没有读取任何字符,在这种情况下您有一个 EOF)。

int nr = read(0, buffer, sizeof buffer);
if (nr > 0) {
  if (buffer[nr - 1] == '\n')
    { /* EOL typed */ }
  else if (nr == sizeof buffer)
    { /* Filled the buffer */ }
  else
    { /* Probably an EOF was typed, not at the start of a line */ }
else if (nr < 0)
  { /* handle error */ }
else
  { /* EOF */ }