c ++混合printf与wprintf(或cout与wcout) [英] c++ Mixing printf with wprintf (or cout with wcout)

查看:500
本文介绍了c ++混合printf与wprintf(或cout与wcout)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你不应该与printf,cout和wprintf,wcout混合打印,但是很难找到一个好的答案为什么,如果它是可能的。问题是我使用外部库打印与printf和我自己使用wcout。如果我做一个简单的例子,它工作正常,但从我的完整的应用程序,它根本不打印printf语句。如果这真的是一个限制,那么会有许多图书馆,不能与广泛的打印应用程序一起工作。

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 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.

更新:

我把它归结为:

#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 :

首先, wprintf出现同样的问题。但我发现添加:

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

std::ios::sync_with_stdio(false);

实际上做的伎俩...(注意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 :-(

推荐答案

应该能够混合它们,但是它们通常使用单独的缓冲机制,因此它们彼此重叠:

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";


hellothis是一个惊奇的世界

hellothis is a suprise world

您没有提供足够的信息来诊断您的应用程序中printf()的问题,但我怀疑您有多个c运行时(代码中有一个,printf()代码中有一个),并且存在冲突。

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.

这篇关于c ++混合printf与wprintf(或cout与wcout)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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