gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:54:31 29 4
gpt4 key购买 nike

我知道您不应该将打印与 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com