你如何 "flush" write() 系统调用?
How do you "flush" the write() system call?
我从用户那里获取输入,然后使用 read() 和 write() 系统调用将其打印到标准输出。出于某种原因,它会在常规输出之外打印一些额外的字符。附加字符来自用户输入,因此它们不是随机的。但是,我假设这可能是由于未清除缓冲区以接收新输入所致。我搜索了许多适合这种情况的不同东西,但我仍然感到困惑。为此,我只使用低级 I/O,因此不会实现任何 C 标准库。我问过的大多数人都说使用 "fflush",但我认为这只适用于 stdio.h 库,我不会使用它。
在此先感谢您提供有关此事的任何建议或资源。
试试这个功能:
eraseFunction()
{
std::cin.clear();
std::cin.ignore(std::cin.rdbuf()->in_avail());
}
所以一旦你读取()一些东西你应该调用这个函数来擦除缓冲区
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);
}
十比一你的第三个参数write
错了。
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.
他们错了,你是对的。
我从用户那里获取输入,然后使用 read() 和 write() 系统调用将其打印到标准输出。出于某种原因,它会在常规输出之外打印一些额外的字符。附加字符来自用户输入,因此它们不是随机的。但是,我假设这可能是由于未清除缓冲区以接收新输入所致。我搜索了许多适合这种情况的不同东西,但我仍然感到困惑。为此,我只使用低级 I/O,因此不会实现任何 C 标准库。我问过的大多数人都说使用 "fflush",但我认为这只适用于 stdio.h 库,我不会使用它。 在此先感谢您提供有关此事的任何建议或资源。
试试这个功能:
eraseFunction()
{
std::cin.clear();
std::cin.ignore(std::cin.rdbuf()->in_avail());
}
所以一旦你读取()一些东西你应该调用这个函数来擦除缓冲区
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()
andwrite()
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);
}
十比一你的第三个参数write
错了。
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.
他们错了,你是对的。