在按下 CTRL+D 之前,我无法持续从标准输入读取内容。我必须使用 unistd.h 中的 read()
。我正在尝试模拟 cat 功能。有没有办法让我的缓冲区(我用 %s 打印)看起来整洁,没有 read(STDIN_FILENO, buf, 256)
中不必要的空格?
最佳答案
I am trying to simulate the cat function.
这是一个开始:
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. */
}
关于c - 从 stdin 读取直到 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15511573/