捕获STDIN

Kon*_*rad 3 c++ svn

在创建subversion repo时,会将许多钩子模板文件放入文件系统中.在检查示例precommit hook时,它详细说明了钩子是由参数传递的信息执行的,看起来也是由STDIN执行的.

# ... Subversion runs this hook by invoking a program
# (script, executable, binary, etc.) named 'pre-commit' (for which
# this file is a template), with the following ordered arguments:
#
#   [1] REPOS-PATH   (the path to this repository)
#   [2] TXN-NAME     (the name of the txn about to be committed)
#
#   [STDIN] LOCK-TOKENS ** the lock tokens are passed via STDIN.
Run Code Online (Sandbox Code Playgroud)

捕获参数是微不足道的,但程序如何捕获STDIN?在int main(...)中运行的以下代码片段无法收集任何内容.

char buffer[1024];
std::cin >> buffer;
buffer[1023] = '\0';
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Kon*_*lph 5

读取逐行输入的最简单方法是以下范例:

std::string line;
while(getline(line, std::cin)) {
    // Do something with `line`.
}
Run Code Online (Sandbox Code Playgroud)

它也是安全,可靠和相对有效的.不要不必要地使用char缓冲区.