When creating a subversion repo a number of hook template files are dropped into the filesystem. On inspection of the example precommit hook it details that the hook is executed with information passed by argument and seemingly by STDIN too.
# ... 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 STD开发者_运维百科IN.
Capturing the arguments is trivial but how would a program trap the STDIN? The following code snippet run in int main(...) fails to collect anything.
char buffer[1024];
std::cin >> buffer;
buffer[1023] = '\0';
What am I doing wrong?
The easiest way to read line by line input is the following paradigm:
std::string line;
while(getline(line, std::cin)) {
// Do something with `line`.
}
it’s also safe, reliable and relatively efficient. Don’t muck around with char buffers needlessly.
精彩评论