fflush(标准输出)在C [英] fflush(stdout) in c

查看:187
本文介绍了fflush(标准输出)在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

权当我在fflush(标准输出)和我分手GDB那里,我能知道什么是有在标准输出之前,我确实打印?

Right when I am at fflush(stdout) and I break there in GDB, can I know what is there in stdout before I actually print it?

我怎么能知道在任何时间点还有什么在标准输出?

How can I know what is there in stdout at any point in time?

推荐答案

您几乎肯定可以,但你可能不应该。该标准仅要求文件是一个类型是非常有用的实施,以确定一个打开的文件和任何国家需要实现的各种功能上的流操作语义

You almost certainly can, but you probably shouldn't. The standard requires only that FILE be a type that is useful to the implementation to identify an open file and whatever state is required to implement the semantics of the various functions that operate on streams.

我一般与其他海报同意fflush()是要知道你居然写信给文件的可靠方法。

I'd generally agree with other posters that fflush() is a reliable way to know what you actually wrote to the file.

不过,如果你已经忘记了你的code的部分可能会被写入流,那么它可以偶尔是有用的观看动作流并抓住它的变化。

However, if you have lost track of what parts of your code might be writing to a stream, then it can occasionally be useful to watch the stream in action and catch it changing.

在实践中,文件是一个typedef一个结构由在头文件中的stdio的实现声明.H(通常命名为结构_iobuf )。虽然典型的实现只是轻轻地记录其成员,一个典型的实现也实现的putchar()和它的一些朋友因为这也是在stdio.h中发现宏。也就是说,与源,你很可能会使用gdb使用任何工具链的C运行时库可能的可用性相结合,让你你需要的引擎盖下偷看的所有信息。

In practice, FILE is a typedef for a struct that is declared by your implementation in the header file stdio.h (often named struct _iobuf). Although a typical implementation only lightly documents its members, a typical implementation also implements putchar() and some of its friends as macros that are also found in stdio.h. That, combined with the likely availability of sources for the C runtime library of any toolchain you are likely to be using with gdb, gets you all the information you need to peek under the hood.

在MinGW的GCC 3.4.5工具文件如下提供的stdio.h中:

The stdio.h provided in MinGW GCC 3.4.5 implements FILE as follows:

typedef struct _iobuf
{
    char*   _ptr;
    int _cnt;
    char*   _base;
    int _flag;
    int _file;
    int _charbuf;
    int _bufsiz;
    char*   _tmpfname;
} FILE;

// oversimplify declaration of _iob[] here for clarity:
extern FILE _iob[FOPEN_MAX];    /* An array of FILE imported from DLL. */
//...
#define STDIN_FILENO    0
#define STDOUT_FILENO   1
#define STDERR_FILENO   2
#define stdin   (&_iob[STDIN_FILENO])
#define stdout  (&_iob[STDOUT_FILENO])
#define stderr  (&_iob[STDERR_FILENO])

和农具的putchar()为内联函数采取GCC扩展的优势,C:

and implements putchar() as an inline function taking advantage of a GCC extension to C:

__CRT_INLINE int __cdecl __MINGW_NOTHROW putchar(int __c)
{
  return (--stdout->_cnt >= 0)
    ?  (int) (unsigned char) (*stdout->_ptr++ = (char)__c)
    :  _flsbuf (__c, stdout);}

从这里就可以告诉缓冲区的末尾由会员 _ptr 指出,并推断唯一的其他的char * 结构_iobuf _base )所指向的缓冲区的开始。会员 _cnt 显然是剩余的未用的缓冲区的字符数。功能 _flsbuf()必须采取不适合,并把它放在缓冲区的开头后,写下了当前缓冲区内容的文件,并恢复第一个字符 _cnt 字段。

From this you can tell that the end of the buffer is pointed to by the member _ptr, and infer that the only other char * in struct _iobuf (_base) is pointing to the beginning of the buffer. The member _cnt is clearly the count of unused characters remaining in the buffer. The function _flsbuf() must take the first character that didn't fit and put it at the beginning of the buffer after it wrote the current buffer content to the file and restored the _cnt field.

所以,如果你看 stdout-> _base BUFSIZ - stdout-> _cnt 你会的,对于此实施方式中,有多少,什么是在当前缓冲区的显示器。

So, if you watch stdout->_base and BUFSIZ - stdout->_cnt you would, for this implementation, have a display of how much and what is in the current buffer.

这篇关于fflush(标准输出)在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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