使用getline获取输入并检查C中是否为CTRL-D

时间:2014-05-10 16:21:14

标签: c

我对c很新,基本上我遇到的问题是目前我有这个

  char* buf = (char*) malloc(sizeof(char) * MAX_LINE);
  size_t n=MAX_LINE;
  getline(&buf, &n, stdin);

如何检查buf以查看何时输入了CTRL-D?

非常感谢

1 个答案:

答案 0 :(得分:1)

^ D将在stdin上发送EOF。

从联系手册:

   On success, getline() and getdelim() return the number of characters read, including the delimiter character, but not includ‐
   ing the terminating null byte.  This value can be used to handle embedded null bytes in the line read.

   Both functions return -1 on failure to read a line (including end-of-file condition).