用C刷新缓冲区 [英] Flushing buffers in C

查看:147
本文介绍了用C刷新缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该 fflush()不能用来刷新缓冲区,即使它是一个输出流?

Should fflush() not be used to flush a buffer even if it is an output stream?

这是什么用呢?我们如何在一般刷新缓冲区?

What is it useful for? How do we flush a buffer in general?

推荐答案

我从来没有听说过不刷新输出缓冲区,我很想听到你的上源。刷新输出缓冲器:

I've never heard not to flush the output buffer, and I would be interested to hear your source on that. Flushing the output buffers:

printf("Buffered, will be flushed");
fflush(stdout); // Prints to screen or whatever your standard out is

fprintf(fd, "Buffered, will be flushed");
fflush(fd);  //Prints to a file

可以是一个非常有用的技术。你为什么要刷新输出缓冲区?通常,当我做到这一点,那是因为code崩溃,我要调试的东西。该标准缓冲液将不打印每次你打电话的printf()等待,直到它满了一次转储一堆。所以,如果你尝试检查,如果你做它在崩溃之前一个函数调用,它有助于的printf 类似来到这里!,有时缓冲撞击发生之前没有被刷新,你不能告诉你真正得到多远。

Can be a very helpful technique. Why would you want to flush an output buffer? Usually when I do it, it's because the code is crashing and I'm trying to debug something. The standard buffer will not print everytime you call printf() it waits until it's full then dumps a bunch at once. So if you're trying to check if you're making it to a function call before a crash, it's helpful to printf something like "got here!", and sometimes the buffer hasn't been flushed before the crash happens and you can't tell how far you've really gotten.

另外,它是有帮助的,时间是在多进程和多线程code。同样,缓冲区并不总是刷新到一个呼叫的printf(),所以如果你想知道多进程的执行真正秩序应该fflush缓冲区以后每隔打印。

Another time that it's helpful, is in multi-process or multi-thread code. Again, the buffer doesn't always flush on a call to a printf(), so if you want to know the true order of execution of multiple processes you should fflush the buffer after every print.

我做一个习惯,这样做,它节省了我调试了很多头痛。我能想到的这样做唯一的缺点是,的printf()是一个昂贵的操作(这就是为什么它默认情况下不刷新缓冲区)。

I make a habit to do it, it saves me a lot of headache in debugging. The only downside I can think of to doing so is that printf() is an expensive operation (which is why it doesn't by default flush the buffer).

至于刷新输入缓冲区(标准输入),你不应该这样做。法拉盛标准输入是根据C11标准定义的操作§7.21.5.2第2部分:

As far as flushing the input buffer (stdin), you should not do that. Flushing stdin is undefined behavior according to the C11 standard §7.21.5.2 part 2:

如果流点到输出流... fflush函数导致该流的任何未写入的数据...要写入该文件;否则,行为是不确定的。

If stream points to an output stream ... the fflush function causes any unwritten data for that stream ... to be written to the file; otherwise, the behavior is undefined.

在某些系统,Linux是一个作为你可以在 fflush手册页看到() ,有一个定义的行为,但它依赖于系统,使您的code将不可移植。

On some systems, Linux being one as you can see in the man page for fflush(), there's a defined behavior but it's system dependent so your code will not be portable.

现在,如果你在你可以使用 fpurge()上输入缓冲区很担心垃圾套牢。
请参见更多href=\"http://www.manpagez.com/man/3/fpurge/\">关于 fflush() fpurge()

Now if you're worried about garbage "stuck" in the input buffer you can use fpurge() on that. See here for more on fflush() and fpurge()

这篇关于用C刷新缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆