因此,谷歌快速搜索fflush(stdin)
清除输入缓冲区会发现许多网站警告不要使用它.然而,这正是我的CS教授教授课程的原因.
使用有多糟糕fflush(stdin)
?即使我的教授正在使用它并且似乎完美无缺地工作,我是否真的应该放弃使用它?
我认为fsync()在内部执行fflush(),因此在流上使用fsync()是可以的.但是我在网络I/O下执行时会得到意想不到的结果.
我的代码片段:
FILE* fp = fopen(file, "wb");
/* multiple fputs() calls like: */
fputs(buf, fp);
...
...
fputs(buf.c_str(), fp);
/* get fd of the FILE pointer */
fd = fileno(fp);
#ifndef WIN32
ret = fsync(fd);
#else
ret = _commit(fd);
fclose(fp);
Run Code Online (Sandbox Code Playgroud)
但似乎_commit()并没有刷新数据(我在Windows上试过并且数据是在Linux导出的文件系统上编写的).
当我将代码更改为:
FILE* fp = fopen(file, "wb");
/* multiple fputs() calls like: */
fputs(buf, fp);
...
...
fputs(buf.c_str(), fp);
/* fflush the data */
fflush(fp);
fclose(fp);
Run Code Online (Sandbox Code Playgroud)
这次它会刷新数据.
我想知道_commit()是否与fflush()做同样的事情.有什么投入?
如果多个线程同时调用fflush()
同一个FILE*
变量,是否会发生任何不良事件(如未定义的行为,文件损坏等)?
澄清:我不是指同时写文件.我只是说要同时冲洗它.
线程不会同时读取或写入文件(它们只在临界区内写入文件,一次一个线程).它们只在临界区外冲洗,以便更快地释放关键部分,以便让其他人完成其他工作(文件写入除外).
虽然可能发生一个线程正在写文件(在临界区内),而另一个线程正在刷新文件(在临界区之外).
如何刷新stdin?
为什么它不能在以下代码片段中工作?
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <fcntl.h>
int main()
{
int i=0,j=0, sat;
char arg[256];
char * argq;
argq = malloc(sizeof(char)*10);
printf("Input the line\n");
i=read(0, arg, sizeof(char)*9);
arg[i-1]='\0';
fflush(stdin);
i=read(0, argq, sizeof(char)*5);
argq[i-1]='\0';
puts(arg);
puts(argq);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我将输入作为11个字符,则只应读取9个,但是stdin中剩余的两个字符不会被刷新并在argq中再次读取.为什么?
输入:123 456 789
产出:123 456 89
为什么我将这89作为输出?
以下是使用fflush()的示例代码:
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
void flush(FILE *stream);
int main(void)
{
FILE *stream;
char msg[] = "This is a test";
/* create a file */
stream = fopen("DUMMY.FIL", "w");
/* write some data to the file */
fwrite(msg, strlen(msg), 1, stream);
clrscr();
printf("Press any key to flush DUMMY.FIL:");
getch();
/* flush the data to DUMMY.FIL without closing it */
flush(stream);
printf("\nFile was flushed, Press any key to quit:");
getch();
return 0;
}
void flush(FILE *stream)
{ …
Run Code Online (Sandbox Code Playgroud) 我在Windows下有一个很好的旧C FILE文件描述符,输出流使用它来写入数据.我的问题很简单,但我找不到答案:
假设我不调用fflush,内容何时被刷新到光盘?
流不断接收数据,似乎内容经常被刷新,但刷新它的规则是什么?
我正在编写一个脚本,它执行如下操作:
echo -n "Doing stuff, wait for it... "
do_stuff
(($?==0)) && echo SUCCESS || echo FAILURE
Run Code Online (Sandbox Code Playgroud)
请原谅糟糕的 bash 技能。无论如何,问题是该行的第一部分在do_stuff
完成之前不会被打印 - 虽然对我来说用户知道我接下来要运行什么很重要。对我来说也很重要,因为我很迂腐,不打印换行符。因此,文本位于缓冲区中并且不会被刷新。
这个问题非常相似,但是OP对事情基本上是满意的。我不是。如果到了紧要关头,我什至愿意使用与诅咒相关的东西(但请记住,这毕竟是一个 shell 脚本)。
我正在阅读'UNIX网络编程:套接字网络API',在示例代码中,它们具有错误处理功能,其中包含以下行:
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(stderr);
Run Code Online (Sandbox Code Playgroud)
其中buf包含错误描述.我不明白为什么在第一行的stdout上使用fflush以及注释解释其使用原因的原因.
Platform: windows 8.1 IDE: vs2013 use c/c++
Process A read stdout of subprocess using pipe redirect.
but subprocess dont invoke fflush after printf, so processs A cant read anything from pipe before subprocess run to end.
ps: I have souce code of subprecess, but its very hard to modify them.
So whether Process A can force subprocess refresh stdout buffer to read something before subprocess run to end? (the same effective as fflush)