开发者

c++ Mixing printf with wprintf (or cout with wcout)

开发者 https://www.devze.com 2022-12-28 06:41 出处:网络
I know you should not mix printing with printf,cout and wprintf,wcout, but have a hard time finding a good answer why and if it is possible to get round it. The problem is I use a external library tha

I know you should not mix printing with printf,cout and wprintf,wcout, but have a hard time finding a good answer why and if it is possible to get round it. The problem is I use a external library that prints with printf and my own uses wcout. If I开发者_C百科 do a simple example it works fine, but from my full application it simply does not print the printf statements. If this is really a limitation, then there would be many libraries out there which can not work together with wide printing applications. Any insight on this is more than welcome.

Update :

I boiled it down to :

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

#include <readline/readline.h>
#include <readline/history.h>

int main()
{
    char *buf;

    std::wcout << std::endl; /* ADDING THIS LINE MAKES PRINTF VANISH!!! */

    rl_bind_key('\t',rl_abort);//disable auto-complete

    while((buf = readline("my-command : "))!=NULL)
    {
        if (strcmp(buf,"quit")==0)
            break;

        std::wcout<<buf<< std::endl;

        if (buf[0]!=0)
            add_history(buf);
    }

    free(buf);

    return 0;
}

So I guess it might be a flushing problem, but it still looks strange to me, I have to check up on it.

Update -> Work around :

First of all, the same problem arise with wprintf. But I found that adding :

std::ios::sync_with_stdio(false);

actually did the trick...(note false and not as I would expect true..), the only thing that bothers me, is that I don't understand why and how to figure it out :-(


I think you're talking about std::ios_base::sync_with_stdio, but IIRC it is on by default.


You should be able to mix them, but they typically use separate buffering mechanisms so they overlap each other:

printf("hello world");
cout << "this is a suprise";

can result in:

hellothis is a suprise world

You don't provide enough information to diagnose your problem with printf() in your application, but I suspect you have more than one c runtime (one in your code, one in the printf() code) and there is a conflict.


The printf() and cout buffers are either synchronised by default, or are in fact the same buffer. If you are having problems with buffering, the obvious solution is to flush the buffer after each output:

fflush( stdout );
cout.flush();

this flushes the buffer(s) to the operating system, and once done there is no possibility of interleaving, or of output being lost.


Libraries should not use printf, cout, or any other I/O to standard handles. They should use a callback routine to delegate output to a method of the main program's choice.

An obvious exception is a library whose sole purpose is output, but then it's the main program's choice to use that. And this rule doesn't forbid I/O to file descriptors opened by the library.

Not only would that solve the issue raised here, but it also takes care of disconnected operation (linux program with no tty, eg run via nohup, or Win32 service).


buffering headaches. typically, you can, though, since they are synced. the people who tell you not to are probably people who remember the pain of using multiple io methods and want to save you from it. (just don't mix either with system calls. that would be painful.)

0

精彩评论

暂无评论...
验证码 换一张
取 消