本文介绍了在同一程序中混合cout和wcout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在阅读C ++ Cookbook,其中包含以下代码片段: // cout< s<< std :: endl; //你不应该能够 wcout<< ws 如果你有兴趣看到实际的例子,的页面中的链接谷歌图书。 另外,我发现这个的 SO问题,似乎声明混合wcout和cout是好的。 从C ++标准[27.4.1]: 相应wide-和窄字符混合操作流遵循相同的语义混合这样的操作上的文件,如ISO C标准的修订1指定 从C标准[7.19.2]: 每个流都有一个方向。后在其上执行任何操作之前一个流与外部文件,但相关联的,该流是无定向。一旦一个宽字符输入/输出功能已应用到流无定向,流变成一个面向宽流。同样,一旦一个字节的输入/输出功能具有被施加到流无定向,流变成一个面向字节的流。 只有调用freopen函数或fwide函数才能改变流的方向。 (成功调用freopen会删除任何方向。) 字节输入/输出函数不应用于宽定向流和宽字符输入/输出函数不应该应用于面向字节的流。 所以,标准似乎说,你不应该混合它们。但是,我发现这篇引文从这篇文章: 对于Visual C ++ 10.0,fwide函数被记录为未实现。从实际的角度来看,至少在输出整行的水平上,它显然可以很好地混合使用cout和wcout。所以,幸运的是,Visual C ++显然只是忽略了标准的要求,并且不保持一个不切实际的显式C FILE流定向。 关于gcc我发现这个报价从此处: 这是一个新功能,而不是错误,请参阅libstdc ++ / 11705并且一般在C标准(C99,7.19.2)中搜索关于流定向的。简而言之,你不能混合面向字节和宽的I / O。现在,由于libstdc ++ / 11705中指出的 ,你可以通过调用std :: ios :: sync_with_stdio(false)来获得接近期望值的东西。 解决方案当 cout 或 wcout 第一次调用时, stdout 的方向设置。在 cout 的情况下, stdout 成为面向字节的流,而在 wcout , stdout 成为面向广泛的流。根据C ++标准[27.4.1]和C标准[7.19.2],一旦流的方向被设置,你不应该调用与该流的方向不兼容的函数。 I was reading the "C++ Cookbook" which had the following snippet:// cout << s << std::endl; // You shouldn't be able towcout << ws << std::endl; // run these at the same timeIf you're interested in seeing the actual example, here is a link to the page on Google books.Also, I found this SO question which seems to state that mixing wcout and cout is okay. Could someone explain to me what this comment is talking about?EDITFrom C++ Standard [27.4.1]: Mixing operations on corresponding wide- and narrow-character streams follows the same semantics as mixing such operations on FILEs, as specified in Amendment 1 of the ISO C standard.From C Standard [7.19.2]: Each stream has an orientation. After a stream is associated with an external file, but before any operations are performed on it, the stream is without orientation. Once a wide character input/output function has been applied to a stream without orientation, the stream becomes a wide-oriented stream. Similarly, once a byte input/output function has been applied to a stream without orientation, the stream becomes a byte-oriented stream. Only a call to the freopen function or the fwide function can otherwise alter the orientation of a stream. (A successful call to freopen removes any orientation.) Byte input/output functions shall not be applied to a wide-oriented stream and wide character input/output functions shall not be applied to a byte-oriented stream.So, the standard seems to say that you should not mix them. However, I found this quote from this article: For Visual C++ 10.0 the fwide function is documented as being unimplemented. And from a practical point of view, at least at the level of outputting whole lines it apparently works fine to intermingle use of cout and wcout. So, happily, Visual C++ apparently just disregards the standard’s requirements and does not maintain an impractical explicit C FILE stream orientation.And also, concerning gcc I found this quote from here: This is a (new) feature, not a bug, see libstdc++/11705 and in general search about stream orientation in the C standard (C99, 7.19.2). In a nutshell you cannot mix byte oriented and wide oriented I/O. For now, due to the bug pointed out in libstdc++/11705, you can obtain something close to your expectations by calling std::ios::sync_with_stdio(false); at the beginning of your program. 解决方案 When cout or wcout is called for the first time, the orientation for stdout becomes set. In the case of cout, stdout becomes a byte-oriented stream, and in the case of wcout, stdout becomes a wide-oriented stream. As per the C++ standard [27.4.1] and C standard [7.19.2], once the orientation of a stream is set, you should not call a function which is not compatible with the orientation of that stream. 这篇关于在同一程序中混合cout和wcout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 03:27