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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You almost certainly can, but you probably shouldn’t. The standard requires only that
FILEbe 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.I’d generally agree with other posters that
fflush()is a reliable way to know what you actually wrote to the file.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.
In practice,
FILEis a typedef for astructthat is declared by your implementation in the header file stdio.h (often namedstruct _iobuf). Although a typical implementation only lightly documents its members, a typical implementation also implementsputchar()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.The stdio.h provided in MinGW GCC 3.4.5 implements
FILEas follows:and implements
putchar()as an inline function taking advantage of a GCC extension to C:From this you can tell that the end of the buffer is pointed to by the member
_ptr, and infer that the only otherchar *instruct _iobuf(_base) is pointing to the beginning of the buffer. The member_cntis 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_cntfield.So, if you watch
stdout->_baseandBUFSIZ - stdout->_cntyou would, for this implementation, have a display of how much and what is in the current buffer.