标签: fflush

使用fflush(stdin)

因此,谷歌快速搜索fflush(stdin)清除输入缓冲区会发现许多网站警告不要使用它.然而,这正是我的CS教授教授课程的原因.

使用有多糟糕fflush(stdin)?即使我的教授正在使用它并且似乎完美无缺地工作,我是否真的应该放弃使用它?

c stdin fflush

69
推荐指数
6
解决办法
4万
查看次数

在C中刷新缓冲区

如果fflush()不用于冲洗,即使它是一个输出流的缓冲区?

它有用的是什么?我们如何刷一般缓冲区?

c buffer fflush

54
推荐指数
1
解决办法
17万
查看次数

fflush和fsync之间的区别

我认为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()做同样的事情.有什么投入?

c windows fsync fflush

50
推荐指数
2
解决办法
4万
查看次数

程序可以同时在同一个FILE*上调用fflush()吗?

如果多个线程同时调用fflush()同一个FILE*变量,是否会发生任何不良事件(如未定义的行为,文件损坏等)?

澄清:我不是指同时写文件.我只是说要同时冲洗它.

线程不会同时读取或写入文件(它们只在临界区内写入文件,一次一个线程).它们只在临界区外冲洗,以便更快地释放关键部分,以便让其他人完成其他工作(文件写入除外).

虽然可能发生一个线程正在写文件(在临界区内),而另一个线程正在刷新文件(在临界区之外).

c multithreading file stdio fflush

43
推荐指数
3
解决办法
1784
查看次数

我无法冲洗stdin

如何刷新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作为输出?

c stdin fflush

17
推荐指数
3
解决办法
5万
查看次数

了解fflush()的必要性以及与之相关的问题

以下是使用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)

c fflush

13
推荐指数
1
解决办法
4万
查看次数

FILE何时刷新?

我在Windows下有一个很好的旧C FILE文件描述符,输出流使用它来写入数据.我的问题很简单,但我找不到答案:

假设我不调用fflush,内容何时被刷新到光盘?

流不断接收数据,似乎内容经常被刷新,但刷新它的规则是什么?

c c++ windows libc fflush

10
推荐指数
1
解决办法
701
查看次数

在 bash 中,如何强制刷新打印到终端的不完整行

我正在编写一个脚本,它执行如下操作:

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 脚本)。

bash terminal newline flush fflush

9
推荐指数
1
解决办法
4701
查看次数

为什么在写入stderr之前我需要在stdout上使用fflush?

我正在阅读'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以及注释解释其使用原因的原因.

c unix stdout stderr fflush

7
推荐指数
1
解决办法
2604
查看次数

How fo force subprocess to refresh stdout buffer?

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)

subprocess fflush

6
推荐指数
0
解决办法
1324
查看次数

标签 统计

fflush ×10

c ×8

stdin ×2

windows ×2

bash ×1

buffer ×1

c++ ×1

file ×1

flush ×1

fsync ×1

libc ×1

multithreading ×1

newline ×1

stderr ×1

stdio ×1

stdout ×1

subprocess ×1

terminal ×1

unix ×1