Question

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)?

Was it helpful?

Solution

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. */
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top