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();

 

 

 

posted on 2026-08-17 09:44  疯狂delphi  阅读(2)  评论(0)    收藏  举报

导航