Gặp lỗi khi đọc file Tiếng Việt trong c++

Chào mọi người. Em có 1 bài tập là đọc file có thông tin phòng thi và tên người thi. Xuất ra phòng thi có số người thi nhiều nhất. Vấn đề em gặp phải đó là khi đọc nó không đọc được chuỗi ký tự.
Em phải làm theo hàm freopen với file dạng bình thường. Với dạng unicode text này em sài hàm _wfreopen nên không làm theo các bài mẫu trên mạng được.


Đây là phần code trích gọn của em.

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable::4996)

#include "ThongTin.h"

int main()
{
  _setmode(_fileno(stdin), _O_WTEXT);
  _setmode(_fileno(stdout), _O_WTEXT);
  _wfreopen(L"Input.txt",L"rt" , stdin);



CThongTin a;
  a.nhap();
  _wfreopen(L"Output.txt", L"wt", stdout);
  a.xuatPhongCoNhieuNguoi();

  return 0;
}
#include "ThongTin.h"

void CThongTin::nhap()
{
  
  wstring temp;
  getline(wcin, temp);
  this->maPhong = temp.substr(0, 2);
  this->hoTen = temp.substr(3, temp.length());
}

khi debug đến câu lệnh getline(wcin,temp); thì em thấy nó đọc không đúng.


Không biết em đã dùng sai hàm gì, hay mọi người có thể giúp em tìm ra nguyên nhân và khắc phục với ạ. Cảm ơn mọi người

Em có thể dùng getline để đọc từng dòng.

2 Likes

Bạn có dùng std::wcout để xuất ra không?

2 Likes

Cảm ơn anh rất nhiều. Lỗi đó là do em chưa thực hiện đoạn code này.

wcin.imbue(utf8_locale);
wcout.imbue(utf8_locale);

Em đã đọc file tiếng việt thành công rồi ạ. Cũng nhờ link của anh. Nhưng em có vấn đề khi thực hiện so sánh 2 đoạn string. Em sẽ đăng bài mới. Mong anh vào giúp đỡ nhé.

3 Likes

Mình sẽ dán code của mình vào. Ai có vấn đề tương tự thì tham khảo nhé.

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable::4996)

#include "ThongTin.h"


int main()
{
	_setmode(_fileno(stdin), _O_U16TEXT);
	_setmode(_fileno(stdout), _O_U16TEXT);
	_wfreopen(L"Input.txt",L"rt" , stdin);
	const std::locale empty_locale = std::locale::empty();
	typedef std::codecvt_utf8<wchar_t> converter_type;
	const converter_type* converter = new converter_type;
	const std::locale utf8_locale = std::locale(empty_locale, converter);
	std::wifstream stream(L"Input.txt");
	stream.imbue(utf8_locale);
	wcin.imbue(utf8_locale);
	wcout.imbue(utf8_locale);
	CThongTin a;
	//a.taoStream("Input.txt", "Output.txt");
	a.nhap();
	_wfreopen(L"Output.txt", L"wt", stdout);
	a.xuatPhongCoNhieuNguoi();

	return 0;
}
#include "ThongTin.h"

void CThongTin::nhap()
{
	wstring temp;
	getline(wcin, temp);
	this->maPhong = temp.substr(0, 3);
	this->hoTen = temp.substr(3, temp.length());
	this->xuLiKhoangTrang();
}

vector<vector<CThongTin>> CThongTin::nhapLienTuc()
{
	vector<vector<CThongTin>> a(1, vector<CThongTin>(1,*this));

	while (!wcin.eof())
	{
		CThongTin b;
		b.nhap();
		int flag = 0;
		for (int i = 0; i < a.size(); i++)
		{
			if (b.maPhong.compare(a[i][0].maPhong) )
			{
				a[i].push_back(b);
				flag = 1;
			}
		}
		if (flag == 0)
		{
			a.push_back(vector<CThongTin>(1, b));
		}
	}
	return a;
}

void CThongTin::xuat()
{
	wcout << hoTen << endl;
}

void CThongTin::xuatPhongCoNhieuNguoi()
{
	vector<vector<CThongTin>> a = this->nhapLienTuc();
	
	int pNhieuNguoi = 0;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i].size() > a[pNhieuNguoi].size())
		{
			pNhieuNguoi = i;
		}
	}

	wcout << a[pNhieuNguoi][0].maPhong << endl;
	for (int i = 0; i < a[pNhieuNguoi].size(); i++)
	{
		a[pNhieuNguoi][i].xuat(); 
	}
}
3 Likes

A post was merged into an existing topic: Topic lưu trữ các post off-topic - version 3

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?