Qt-标准输入输出stdout、stdin
文章来源:https://blog.csdn.net/halo_hsuh/article/details/120627531
实例代码:
1 #include <iostream> 2 #include <string> 3 class MyClass 4 { 5 friend std::istream& operator>>(std::istream&, MyClass&); 6 friend std::ostream& operator<<(std::ostream&, const MyClass&); 7 public: 8 MyClass() { } 9 ~MyClass() {} 10 private: 11 int age; 12 std::string name; 13 }; 14 std::istream& operator>>(std::istream& in, MyClass& m) 15 { 16 in >> m.name >> m.age; 17 } 18 std::ostream& operator<<(std::ostream& out, const MyClass& m) 19 { 20 out << "name: " << m.name << ", age: " << m.age; 21 } 22 23 24 #include "myclass.h" 25 #include <iostream> 26 27 int main() 28 { 29 MyClass m; 30 std::cin >> m; 31 std::cout<<m; 32 return 0; 33 }
问题现象:在使用的过程中,比如同一个代码在windows上时没有问题,移到kylin系统中就可能出现乱码问题。
问题原因:这是因为写入程序设置的是UTF-8,但读取程序未指定编码格式。
处理方法:设置UTF-8格式读写数据。
1 QTextStream qin(stdin); 2 qin.setCodec("UTF-8"); // 设置接收编码。 3 4 QString data = qin.readLins(); 5 6 发送方 7 QString str; 8 str.toUtf8();
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我
浙公网安备 33010602011771号