Path: utzoo!attcan!uunet!zephyr.ens.tek.com!tektronix!sequent!mntgfx!lisch From: lisch@lisch.mentor.com (Ray Lischner) Newsgroups: comp.lang.c++ Subject: Re: Cout vs *Printf() Message-ID: Date: 23 May 90 20:06:55 GMT References: <3987@castle.ed.ac.uk> <6170022@hpindda.HP.COM> Sender: lisch@mentor.com (Ray Lischner) Distribution: comp Organization: Mentor Graphics Corp., Beaverton, OR Lines: 31 In-reply-to: hardin@hpindda.HP.COM's message of 23 May 90 00:31:19 GMT > > Formatting with cout seems pretty painful. Is it possible to > > use *printf() instead of cout and still maintain the program > > portability? And is it possible mix up both cout and *printf()? > > > > S. Manoharan Janet: sam@uk.ac.ed.lfcs > ---------- > > Mixing cout and printf can be dangerous due (I believe) to different > buffering schemes. The order of things printed may surprise you. > I have even seen printf output completely lost when I mixed couts > and printfs. I have found that I can avoid problems, however, by > alway flushing printf output before using cout and vice versa. From the AT&T C++ Language System Library Manual, Appendix A, Iostream Library Manual Pages, page 7 of IOS(3C++): ios::sync_with_stdio() Solves problems that arise when mixing stdio and iostreams. The first time it is called it will reset the standard iostreams (cin, cout, cerr, clog) to be streams using stdiobufs. After that input and output using these streams may be mixed with input and output using the corresponding FILEs (stdin, stdout, stderr) and will be properly synchronized. sync_with_stdio() makes cout and cerr unit buffered (see ios::unitbuf and ios::stdio above). Invoking sync_with_stdio() degrades performance a variable amount, depending on the length of the strings being inserted (shorter strings incur a larger performance hit). -- Ray Lischner UUCP: {uunet,apollo,decwrl}!mntgfx!lisch -- Ray Lischner UUCP: {uunet,apollo,decwrl}!mntgfx!lisch