cout wcout ifstream 处理中文时的问题

本文介绍了如何使用wcout输出中文字符以及解决ifstream读取中文路径的问题。提供了在GCC编译器下wcout输出中文的具体解决方案,并给出了ifstream正确读取中文路径的方法。

 1.cout在设计时具有智能判断功能,不论是char*类型,还是string类型,都能得到正确的结果。
2.wcout指定要输出宽字符,每个字符占两个字节,我们所用的字符串如:"hello",要想让其按宽字符处理,必须加L,即L"hello",中文也是这样要求。
3.GCC编译器不支持wcout关键字。

 

a、wcout输出中文时,无结果输出

b、ifstream读取中文路径时,并不能正确读取

 

解决办法:

a、

std::wcout.imbue(std::locale(""));
wcout<<_T("你好")<<endl;

b、

std::locale loc = std::locale::global(std::locale(""));//设置环境为系统环境
std::ifstream("c://文件.txt", ios::binary |  ios::in);
std::locale::
global( loc );//注意一定要加此句,以恢复环境,不然后面的操作可能会有问题,如可能有些纯英文路径的文件读入不正确

 

#include <iostream> #include <fstream> #include <string> #include <list> using namespace std; class Contact { public: string name; string address; string phone; string zipCode; Contact(string n = "", string a = "", string p = "", string z = "") : name(n), address(a), phone(p), zipCode(z) {} }; class AddressBook { private: list<Contact> contacts; public: void addContact() { string name, address, phone, zipCode; cout << "Enter Name: "; getline(cin, name); cout << "Enter Address: "; getline(cin, address); cout << "Enter Phone: "; getline(cin, phone); cout << "Enter Zip Code: "; getline(cin, zipCode); contacts.emplace_back(name, address, phone, zipCode); cout << "Contact added successfully." << endl; } void displayContacts() { if (contacts.empty()) { cout << "No contacts to display." << endl; return; } for (const auto& contact : contacts) { cout << "Name: " << contact.name << endl; cout << "Address: " << contact.address << endl; cout << "Phone: " << contact.phone << endl; cout << "Zip Code: " << contact.zipCode << endl; cout << "-----------------------------" << endl; } } void saveToFile(const string& filename) { ofstream file(filename); if (!file.is_open()) { cout << "Error opening file." << endl; return; } for (const auto& contact : contacts) { file << contact.name << "|" << contact.address << "|" << contact.phone << "|" << contact.zipCode << endl; } file.close(); cout << "Contacts saved to file." << endl; } void loadFromFile(const string& filename) { ifstream file(filename); if (!file.is_open()) { cout << "Error opening file." << endl; return; } string line; while (getline(file, line)) { size_t pos1 = line.find('|'); size_t pos2 = line.find('|', pos1 + 1); size_t pos3 = line.find('|', pos2 + 1); if (pos1 != string::npos && pos2 != string::npos && pos3 != string::npos) { string name = line.substr(0, pos1); string address = line.substr(pos1 + 1, pos2 - pos1 - 1); string phone = line.substr(pos2 + 1, pos3 - pos2 - 1); string zipCode = line.substr(pos3 + 1); contacts.emplace_back(name, address, phone, zipCode); } } file.close(); cout << "Contacts loaded from file." << endl; } void searchContact() { string name; cout << "Enter Name to search: "; getline(cin, name); bool found = false; for (const auto& contact : contacts) { if (contact.name == name) { cout << "Name: " << contact.name << endl; cout << "Address: " << contact.address << endl; cout << "Phone: " << contact.phone << endl; cout << "Zip Code: " << contact.zipCode << endl; found = true; } } if (!found) { cout << "Contact not found." << endl; } } void modifyContact() { string name; cout << "Enter Name to modify: "; getline(cin, name); for (auto& contact : contacts) { if (contact.name == name) { cout << "Enter new Address: "; getline(cin, contact.address); cout << "Enter new Phone: "; getline(cin, contact.phone); cout << "Enter new Zip Code: "; getline(cin, contact.zipCode); cout << "Contact modified successfully." << endl; return; } } cout << "Contact not found." << endl; } }; int main() { AddressBook book; int choice; string filename = "contacts.txt"; while (true) { cout << "\nAddress Book Menu:\n"; cout << "1. Add Contact\n"; cout << "2. Display Contacts\n"; cout << "3. Save to File\n"; cout << "4. Load from File\n"; cout << "5. Search Contact\n"; cout << "6. Modify Contact\n"; cout << "7. Exit\n"; cout << "Enter your choice: "; cin >> choice; cin.ignore(); switch (choice) { case 1: book.addContact(); break; case 2: book.displayContacts(); break; case 3: book.saveToFile(filename); break; case 4: book.loadFromFile(filename); break; case 5: book.searchContact(); break; case 6: book.modifyContact(); break; case 7: return 0; default: cout << "Invalid choice. Try again." << endl; } } return 0; } 将此代码中的输出全部改为中文输出
06-16
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值