c++ - 如何将 write() 系统调用设置为 "flush"?

标签 c++ io low-level-io

我从用户那里获取输入,然后使用 read() 和 write() 系统调用将其打印到标准输出。由于某种原因,除了常规输出之外,它还会打印一些额外的字符。附加字符来自用户输入,因此它们不是随机的。但是,我假设这可能是由于缓冲区未清除以接受新输入所致。我搜索了许多适合这种情况的不同东西,但我仍然很困惑。我为此仅使用低级 I/O,因此不会实现任何 C 标准库。我问过的大多数人都说使用“fflush”,但我认为这只适用于 stdio.h 库,我不会使用它。 预先感谢您就此事提供任何建议或资源。

最佳答案

How do you “flush” the write() system call?

你不知道。这是一个系统调用。您没有任何要刷新的应用程序端缓冲区。

I am taking input from the user and then printing it to the standard output using read() and write() system calls. For some reason, it prints a few extra characters in addition to the regular output.

您的代码中有一个错误。它应该看起来像这样:

int count;
while ((count = read(inFD, buffer, sizeof buffer)) > 0)
{
    write(outFD, buffer, count);
}

十比一,你的第三个参数错误。

The majority of people I've asked have said use "fflush", but I thought that would only be applicable with the stdio.h library, which I will not be using.

他们错了,你是对的。

关于c++ - 如何将 write() 系统调用设置为 "flush"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47029139/

相关文章:

c++ - 从文件中读取图像 - C++

c++ - 递归调用以获取带有前缀或后缀参数的 arr 元素的总和

c++ - boost-variant的 vector

Java 是否 InputSream.read(byte[] b, int offset, int length) 等待指定的全部字节数

c# 如何发送仍在写入的文件流并一直发送到创建结束

c - 为什么我的 C 程序不打印任何内容?

c++ - QML ListView 和 c++ : ReferenceError Model not defined

java - 在 Windows 中保存文件

c - 如何使用 read() 重新创建 receive() ?