문제

I am having trouble reading from stdin constantly until CTRL+D is pressed. I have to use read() from unistd.h. I am trying to simulate the cat function. Is there a way to make my buffer (which I print with %s) to look neat without the unnecessary spaces from read(STDIN_FILENO, buf, 256)?

도움이 되었습니까?

해결책

I am trying to simulate the cat function.

Here is a start:

ssize_t nread, nwrite;

while ((nread = read(STDIN_FILENO, buf, sizeof buf)) > 0) {
    nwrite = write(STDOUT_FILENO, buf, nread);
    /* Error handling and partial writes left as exercise. */
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top