STDIN capture

When creating a subversion repository, several template template files are uploaded to the file system. When checking the precommit hook example, it indicates that the hook is being executed with the information passed by the argument and, apparently, also 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.

Capturing arguments is trivial, but how will the program catch STDIN? The following code fragment, executed in int main (...), cannot collect anything.

char buffer[1024];
std::cin >> buffer;
buffer[1023] = '\0';

What am I doing wrong?

+3
source share
1 answer

The easiest way to read line by line is the following paradigm:

std::string line;
while(getline(line, std::cin)) {
    // Do something with `line`.
}

It is also safe, reliable and relatively effective. Do not go without char buffers.

+5
source

Source: https://habr.com/ru/post/1777471/


All Articles