c++ 将 printf 与 wprintf 混合(或将 cout 与 wcout 混合)

标签 c++

我知道您不应该将打印与 printf,cout 和 wprintf,wcout 混合使用,但是很难找到一个好的答案,为什么以及是否可以绕过它。问题是我使用了一个用 printf 打印的外部库,而我自己使用了 wcout。如果我做一个简单的例子,它工作正常,但从我的完整应用程序来看,它根本不打印 printf 语句。如果这真的是一个限制,那么就会有许多库无法与广泛的打印应用程序一起工作。我们非常欢迎对此有任何见解。

更新:

我把它归结为:

#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;
}

所以我想这可能是冲水问题,但我还是觉得很奇怪,我必须检查一下。

更新 -> 解决方法:

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

std::ios::sync_with_stdio(false);

实际上成功了......(注意是假的,而不是我期望的那样......),唯一困扰我的是,我不明白为什么以及如何弄清楚它:-(

最佳答案

我认为您在谈论 std::ios_base::sync_with_stdio,但 IIRC 默认情况下它是打开的。

关于c++ 将 printf 与 wprintf 混合(或将 cout 与 wcout 混合),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2708482/

相关文章:

c++ - GetLogicalDriveStrings,为什么是字符串列表

c++ - 返回对此的右值引用的正确方法

c++ - 在 C++ 中,如何使用类成员来保存从基类派生的任何对象?

c++ - 在不知道套接字的情况下关闭网络连接

c++ - 如何重载自定义 std::sort 比较函数?

c# - 在 C++ 中定义的以下结构的 C# 等价物是什么

c++ - 如何使模板原型(prototype)继承,以便所有特化都继承相同的类/接口(interface)?

c++ - 在.h 中声明名称但在 Xcode 的 .cpp 中报告未声明

c++ - GNU 编译器警告 "class has virtual functions but non-virtual destructor"

c++ - 在没有 friend 的情况下在 C++ 中测试私有(private)类成员