Имя пользователя:
Пароль:  
Помощь | Регистрация | Забыли пароль?  

Название темы: Кирилица в консоли
Показать сообщение отдельно

Аватара для ganselo

Старожил


Сообщения: 232
Благодарности: 90

Профиль | Сайт | Отправить PM | Цитировать


Вот вариант своего класса для ввода/вывода на кириллице
(для Visual C++ 5.0 и более поздних)
Код: Выделить весь код
#ifndef CYR_IOS_H
#define CYR_IOS_H
#include <iostream>
#include <iomanip>
#include <string>
#include <windows.h>
using namespace std;

#define MAX_STR_LEN 4096

//////////////////////////////////////////////////////////////////////////////////
// Класс CyrOstream
class CyrOstream : public ostream {
	public:
		CyrOstream(_Uninitialized no_init) : ostream(no_init) {};
		CyrOstream& operator <<(_Myt& (_cdecl *_f)(_Myt&));
		CyrOstream& operator <<(ios_base& (_cdecl *_f)(ios_base& ));
		CyrOstream& operator <<(short n) { cout << n; return *this; }
		CyrOstream& operator <<(unsigned short n){ cout << n; return *this; }
		CyrOstream& operator <<(int n) { cout << n; return *this; }
		CyrOstream& operator <<(unsigned int n){ cout << n; return *this; }
		CyrOstream& operator <<(long n) { cout << n; return *this; }
		CyrOstream& operator <<(unsigned long n) { cout << n; return *this; }
		CyrOstream& operator <<(float f) { cout << f; return *this; }
		CyrOstream& operator <<(double f) { cout << f; return *this; }
		CyrOstream& operator <<(long double f) { cout << f; return *this; }
		CyrOstream& operator <<(const void* v) { cout << v; return *this; }
		CyrOstream& operator <<(const char*);
		CyrOstream& operator <<(const unsigned char* s) { operator <<((const char*)s); return *this; }
		CyrOstream& operator <<(const signed char* s) { operator <<((const char*)s); return *this; }
		CyrOstream& operator <<(char);
		CyrOstream& operator <<(unsigned char);
		CyrOstream& operator <<(signed char c) { operator <<((char)c); return *this; }
		CyrOstream& put(char);
		CyrOstream& write(const char*, int);
		CyrOstream& write(const unsigned char* s, int len) 
		{	
			write((const char*)s, len); return *this; 
		}
		// Замещение методов класса ios
		long setf(long lFlags) { return cout.setf(lFlags); }
		void unsetf(long lFlags) { cout.unsetf(lFlags); }
		char fill(char cFill) { return cout.fill(cFill); }
		char fill() { return cout.fill(); }
		int precision(int np) { return cout.precision(np); }
		int precision() const { return cout.precision(); }
		int width(int nw) { return cout.width(nw); }
		int width() const { return cout.width(); }
		int rdstate() const { return cout.rdstate(); }
		long flags() const { return cout.flags(); }
		long flags(long _l) { return cout.flags(_l); }
		streambuf* rdbuf() const { return cout.rdbuf(); }
		// Дружественная функция для поддержки параметризованных манипуляторов
		friend CyrOstream& operator <<(CyrOstream&, const _Smanip<int>&);
	private:
		char buf_[MAX_STR_LEN];
};

////////////////////////////////
// Шаблон для вывода типа string
template<class _E, class _Tr, class _A>
inline CyrOstream& operator <<(CyrOstream& os, const basic_string<_E, _Tr, _A>& _X) 
{
	string temp(_X);
	unsigned char symb[2];
	symb[1] = 0;
	for (int i = 0; i < temp.size(); i++) 
	{
		symb[0] = temp.at(i);
		if (symb[0] > 191)
		CharToOem((const char*)symb, (char*)symb);
		cout << symb;
	}
return os;
}

///////////////////////////////////////////////////////////
// Класс CyrIstream
class CyrIstream : public istream {
	public:
		CyrIstream(_Uninitialized no_init) : istream(no_init) {};
		CyrIstream& operator >>(ios_base& (_cdecl *_f)(ios_base& ));
		CyrIstream& operator >>(char*);
		CyrIstream& operator >>(unsigned char* s)
		{
			operator >>((char*)s); return *this;
		}
		CyrIstream& operator >>(signed char* s) 
		{
			operator >>((char*)s); return *this; 
		}
		CyrIstream& operator >>(char& c);
		CyrIstream& operator >>(unsigned char& c)
		{
			operator >>((char&)c); return *this;
		}
		CyrIstream& operator >>(signed char& c)
		{
			operator >>((char&)c); return *this;
		}
		CyrIstream& operator >>(short &n) { cin >> n; return *this; }
		CyrIstream& operator >>(unsigned short &n)
		{
			cin >> n; return *this; 
		}
		CyrIstream& operator >>(int &n) { cin >> n; return *this; }
		CyrIstream& operator >>(unsigned int &n)
		{
			cin >> n; return *this;
		}
		CyrIstream& operator >>(long &n) { cin >> n; return *this; }
		CyrIstream& operator >>(unsigned long &n)
		{
			cin >> n; return *this; 
		}
		CyrIstream& operator >>(float &f) { cin >> f; return *this; }
		CyrIstream& operator >>(double &f) { cin >> f; return *this; }
		CyrIstream& operator >>(long double &f)
		{
			cin >> f; return *this;
		}
		int get() { return cin.get(); }
		CyrIstream& get(char&);
		CyrIstream& get(char*, int, char='\n');
		CyrIstream& get(unsigned char*, int, char = '\n');
		CyrIstream& getline(char*, int, char='\n');
		CyrIstream& getline(unsigned char* pch, int nCount, char delim ='\n') 
		{
			getline((char*)pch, nCount, delim); return *this; 
		}
		CyrIstream& read(char*, int);
		CyrIstream& read(unsigned char* pch, int nCount)
		{
			read((char*)pch, nCount); return *this; 
		}
		CyrIstream& ignore(int nCount = 1, int delim = EOF)
		{
			cin.ignore(nCount, delim); return *this;
		}
		int peek() { return cin.peek(); }
		int gcount() const { return cin.gcount(); }
		CyrIstream& putback(char ch) { cin.putback(ch); return *this; }
		// Замещение методов класса ios
		void clear(int nState = 0) { cin.clear(nState); }
		long setf(long lFlags) { return cin.setf(lFlags); }
		void unsetf(long lFlags) { cin.unsetf(lFlags); }
		int rdstate() const { return cin.rdstate(); }
		long flags() const { return cin.flags(); }
		streambuf* rdbuf() const { return cin.rdbuf(); }
		// Дружественная функция для поддержки параметризованных манипуляторов
		friend CyrIstream& operator >>(CyrIstream&, const _Smanip<int>&);
	private:
		char buf_[MAX_STR_LEN];	
};

/////////////////////////////////////////////////////////////
//Шаблон для ввода типа string
template<class _E, class _Tr, class _A>
inline CyrIstream& operator >>(CyrIstream& is, basic_string<_E, _Tr, _A>& _X) 
{
	string temp;
	cin >> temp;
	unsigned int n = temp.size();
	char *buf = new char[n+1];
	temp.copy(buf, n); buf[n] = 0;
	OemToChar(buf, (char*)buf);
	_X = string(buf);
	delete [] buf;
return is;
}

CyrOstream& CyrOstream::operator << (_Myt& (_cdecl *_f)(_Myt&))
{
	cout << _f; return *this;
}
CyrOstream& CyrOstream::operator << (ios_base& (_cdecl *_f)(ios_base& ))
{
	cout << _f; return *this;
}

CyrOstream& CyrOstream::operator <<(const char *s)
{
	int n = strlen(s);
	strncpy(buf_, s, n); buf_[n] = 0;
	CharToOem(buf_, buf_);
	cout << buf_;
return *this;
}

CyrOstream& CyrOstream::operator <<(char c)
{
	buf_[0] = c; buf_[1] = 0;
	CharToOem(buf_, buf_);
	cout << buf_;
return *this;
}

CyrOstream& CyrOstream::operator <<(unsigned char c)
{
	unsigned char buf[2];
	buf[0] = c; buf[1] = 0;
	if (c > 191)
		CharToOem((const char*)buf, (char*)buf);
	cout << buf;
return *this;
}

CyrOstream& CyrOstream::put(char c)
{
	buf_[0] = c; buf_[1] = 0;
	CharToOem(buf_,  buf_);
	cout.put(buf_[0]);
return *this;
}

CyrOstream& CyrOstream::write(const char* s, int len)
{
	int n = strlen(s);
	strncpy(buf_, s, n); buf_[n] = 0;
	CharToOem(buf_, buf_);
	cout.write(buf_, len);
return *this;
}
CyrOstream& operator <<(CyrOstream& os, const _Smanip<int>& m)
{
	cout << m; return os;
}

CyrIstream& CyrIstream::operator >> (ios_base& (_cdecl *_f)(ios_base& ))
{
	cin >> _f; return *this;
}
CyrIstream& CyrIstream::operator >>(char* s) 
{
	string temp;
	cin >> temp;
	unsigned int n = temp.size();
	temp.copy(buf_, n); buf_[n] = 0;
	OemToChar(buf_, buf_);
	strncpy (s, buf_, n + 1);
return *this;
}

CyrIstream& CyrIstream::operator >>(char& c)
{
	cin >> buf_[0];
	buf_[1] = 0;
	OemToChar(buf_, buf_);
	c = buf_[0];
return *this;
}


CyrIstream& CyrIstream::get(char& symb)
{
	cin.get(buf_[0]);
	buf_[1] = 0;
	OemToChar(buf_, buf_);
	symb = buf_[0];
return *this;
}

CyrIstream& CyrIstream::get(char* pch, int nCount, char delim)
{
	cin.get(pch, nCount, delim);
	OemToChar(pch, pch);
return *this;
}

CyrIstream& CyrIstream::get(unsigned char* pch, int nCount, char delim)
{
	cin.get((char*)pch, nCount, delim);
	OemToChar((const char*)pch, (char*)pch);
return *this;
}

CyrIstream& CyrIstream::getline(char* pch, int nCount, char delim)
{
	cin.getline(pch, nCount, delim);
	OemToChar(pch, pch);
return *this;
}

CyrIstream& CyrIstream::read(char* pch, int nCount)
{
	cin.read(buf_, nCount);
	buf_[nCount] = 0;
	OemToChar(buf_, buf_);
	for(int i = 0; i < nCount; i++)
		pch[i] = buf_[i];
return *this;
}

CyrIstream& operator >>(CyrIstream& is, const _Smanip<int>& m) 
{
	cin >> m; return is;
}

//////////////////////////////////////////////////////////
// Глобальные объекты для ввода и вывода
CyrIstream Cin(_Noinit);
CyrOstream Cout(_Noinit);


///////////////////////////////////////////////////////////
extern CyrIstream Cin;
extern CyrOstream Cout;
#endif

#ifndef CYR_IOS_IMPLEMENTATION
	#define cin Cin
	#define cout Cout
	#define istream CyrIstream
	#define ostream CyrOstream
#endif
Вот вариант для компилятора mingw (всякие там Code::Blocks, Dev C++ и т.д)
Код: Выделить весь код
#ifndef RUS_H_INCLUDED
#define RUS_H_INCLUDED
#include <iostream>
#include <iomanip>
#include <string>
#include <windows.h>
#define MAX_STR_LEN 4096

class CyrOstream : public std::ostream
{
    public:
        CyrOstream() : std::ostream() {}
        CyrOstream& operator <<(__ostream_type& (*__pf)(__ostream_type&))
        {
            std::cout << __pf; return *this;
        }
        CyrOstream& operator <<(__ios_type& (*__pf)(__ios_type&))
        {
            std::cout << __pf; return *this;
        }
        CyrOstream& operator <<(ios_base& (*__pf)(ios_base&))
        {
            std::cout << __pf; return *this;
        }
        CyrOstream& operator <<(short n) { std::cout << n; return *this; }
		CyrOstream& operator <<(unsigned short n) { std::cout << n; return *this; }
		CyrOstream& operator <<(int n) { std::cout << n; return *this; }
		CyrOstream& operator <<(unsigned int n) { std::cout << n; return *this; }
		CyrOstream& operator <<(long n) { std::cout << n; return *this; }
		CyrOstream& operator <<(long long n) {std::cout << n; return *this; }
		CyrOstream& operator <<(unsigned long n) { std::cout << n; return *this; }
		CyrOstream& operator <<(float f) { std::cout << f; return *this; }
		CyrOstream& operator <<(double f) { std::cout << f; return *this; }
		CyrOstream& operator <<(long double f) { std::cout << f; return *this; }
		CyrOstream& operator <<(const void* v) { std::cout << v; return *this; }
		CyrOstream& operator <<(const char*);
		CyrOstream& operator <<(const unsigned char* s) { std::cout << s; return *this;}
		CyrOstream& operator <<(const signed char* s) { std::cout << s; return *this;}
		CyrOstream& operator <<(char);
		CyrOstream& operator <<(unsigned char);
		CyrOstream& operator <<(signed char c) { operator <<((char)c); return *this; }
		CyrOstream& operator <<(std::streambuf *buf) { std::cout << buf; return *this; }

		CyrOstream& put(char);
		CyrOstream& write(const char*, int);
		CyrOstream& write(const unsigned char* s, int len);

        char fill(char cFill) { return std::cout.fill(cFill); }
		char fill() { return std::cout.fill(); }
		int precision(int np) { return std::cout.precision(np); }
		int precision() const { return std::cout.precision(); }
		int width(int nw) { return std::cout.width(nw); }
		int width() const { return std::cout.width(); }
		int rdstate() const { return std::cout.rdstate(); }

        friend CyrOstream& operator <<(CyrOstream&, std::_Setprecision __f);
		friend CyrOstream& operator <<(CyrOstream&, std::_Resetiosflags __f);
		friend CyrOstream& operator <<(CyrOstream&, std::_Setiosflags __f);
		friend CyrOstream& operator <<(CyrOstream&, std::_Setbase __f);
		friend CyrOstream& operator <<(CyrOstream&, std::_Setfill<wchar_t> __f);
		friend CyrOstream& operator <<(CyrOstream&, std::_Setw __f);

		template<typename _CharT, typename _Traits, typename _Alloc>
        friend CyrOstream& operator <<(CyrOstream& os,
            const std::basic_string<_CharT, _Traits, _Alloc>& __str);
    private:
        char buf_[MAX_STR_LEN];
};
//=======================================================================
//=======================================================================
class CyrIstream : public std::istream
{
    public:
        CyrIstream() : std::istream() {}
        CyrIstream& operator >>(ios_base& (*__pf)(ios_base&))
        {
            std::cin >> __pf; return *this;
        }
        CyrIstream& operator >>(char*);
        CyrIstream& operator >>(unsigned char* s)
		{
			operator >>((char*)s); return *this;
		}
		CyrIstream& operator >>(signed char* s)
		{
			operator >>((char*)s); return *this;
		}
		CyrIstream& operator >>(char& c);
		CyrIstream& operator >>(unsigned char& c)
		{
			operator >>((char&)c); return *this;
		}
		CyrIstream& operator >>(signed char& c)
		{
			operator >>((char&)c); return *this;
		}
		CyrIstream& operator >>(short &n) { std::cin >> n; return *this; }
		CyrIstream& operator >>(unsigned short &n)
		{
			std::cin >> n; return *this;
		}
		CyrIstream& operator >>(int &n) { std::cin >> n; return *this; }
		CyrIstream& operator >>(unsigned int &n)
		{
			std::cin >> n; return *this;
		}
		CyrIstream& operator >>(long &n) { std::cin >> n; return *this; }
		CyrIstream& operator >>(unsigned long &n)
		{
			std::cin >> n; return *this;
		}
		CyrIstream& operator >>(float &f) { std::cin >> f; return *this; }
		CyrIstream& operator >>(double &f) { std::cin >> f; return *this; }
		CyrIstream& operator >>(long double &f)
		{
			std::cin >> f; return *this;
		}
		int get() { return std::cin.get(); }
		CyrIstream& get(char&);
		CyrIstream& get(char*, int, char='\n');
		CyrIstream& get(unsigned char*, int, char = '\n');
		CyrIstream& getline(char*, int, char='\n');
		CyrIstream& getline(unsigned char* pch, int nCount, char delim ='\n')
		{
			getline((char*)pch, nCount, delim); return *this;
		}
		CyrIstream& read(char*, int);
		CyrIstream& read(unsigned char* pch, int nCount)
		{
			read((char*)pch, nCount); return *this;
		}
		CyrIstream& ignore(int nCount = 1, int delim = EOF)
		{
			std::cin.ignore(nCount, delim); return *this;
		}
		int peek() { return std::cin.peek(); }
		int gcount() const { return std::cin.gcount(); }
		CyrIstream& putback(char ch) { std::cin.putback(ch); return *this; }

		// Замещение методов класса ios
		//void clear(int nState = 0) { std::cin.clear(nState); }
		//long setf(long lFlags) { return std::cin.setf(lFlags); }
		//void unsetf(long lFlags) { std::cin.unsetf(lFlags); }
		int rdstate() const { return std::cin.rdstate(); }
		long flags() const { return std::cin.flags(); }
		std::streambuf* rdbuf() const { return std::cin.rdbuf(); }

        friend CyrIstream& operator >>(CyrIstream&, std::_Setprecision __f);
		friend CyrIstream& operator >>(CyrIstream&, std::_Resetiosflags __f);
		friend CyrIstream& operator >>(CyrIstream&, std::_Setiosflags __f);
		friend CyrIstream& operator >>(CyrIstream&, std::_Setbase __f);
		friend CyrIstream& operator >>(CyrIstream&, std::_Setfill<wchar_t> __f);
		friend CyrIstream& operator >>(CyrIstream&, std::_Setw __f);

		template<typename _CharT, typename _Traits, typename _Alloc>
        friend CyrIstream& operator >>(CyrIstream& os,
            const std::basic_string<_CharT, _Traits, _Alloc>& __str);
	private:
		char buf_[MAX_STR_LEN];
};

//=======================================================================
//Методы класса CyrOstream
//=======================================================================
CyrOstream& CyrOstream::operator <<(const char *s)
{
    int n = strlen(s);
    strncpy(buf_, s, n); buf_[n] = '\0';
    CharToOem(buf_, buf_);
    std::cout << buf_;
    return *this;
}
//=======================================================================
CyrOstream& CyrOstream::operator <<(char c)
{
	buf_[0] = c; buf_[1] = 0;
	CharToOem(buf_, buf_);
	std::cout << buf_;
return *this;
}
//=======================================================================
CyrOstream& CyrOstream::operator <<(unsigned char c)
{
	unsigned char buf[2];
	buf[0] = c; buf[1] = 0;
	if (c > 191)
		CharToOem((const char*)buf, (char*)buf);
	std::cout << buf;
    return *this;
}
//======================================================================
//======================================================================
CyrOstream& CyrOstream::put(char c)
{
    buf_[0] = c; buf_[1] = 0;
	CharToOem(buf_,  buf_);
	std::cout.put(buf_[0]);
    return *this;
}
//======================================================================
CyrOstream& CyrOstream::write(const char* s, int len)
{
	int n = strlen(s);
	strncpy(buf_, s, n); buf_[n] = 0;
	CharToOem(buf_, buf_);
	std::cout.write(buf_, len);
return *this;
}
//======================================================================
CyrOstream& CyrOstream::write(const unsigned char* s, int len)
{
    write((const char*)s, len);
    return *this;
}
//======================================================================
CyrOstream& operator <<(CyrOstream &os, std::_Setprecision __f)
{
    std::cout << __f;
    return os;
}
//======================================================================
CyrOstream& operator <<(CyrOstream &os, std::_Resetiosflags __f)
{
    std::cout << __f;
    return os;
}
//======================================================================
CyrOstream& operator <<(CyrOstream &os, std::_Setiosflags __f)
{
    std::cout << __f;
    return os;
}
//======================================================================
CyrOstream& operator <<(CyrOstream &os, std::_Setbase __f)
{
    std::cout << __f;
    return os;
}
//======================================================================
CyrOstream& operator <<(CyrOstream &os, std::_Setfill<wchar_t> __f)
{
    std::cout << __f._M_c;
    return os;
}
//======================================================================
CyrOstream& operator <<(CyrOstream &os, std::_Setw __f)
{
    std::cout << __f;
    return os;
}
//======================================================================
template<typename _CharT, typename _Traits, typename _Alloc>
CyrOstream& operator <<(CyrOstream& os,
    const std::basic_string<_CharT, _Traits, _Alloc>& __str)
{
    std::string temp(__str);
	unsigned char symb[2];
	symb[1] = 0;
	for (int i = 0; i < temp.size(); i++)
	{
		symb[0] = temp.at(i);
		if (symb[0] > 191)
		CharToOem((const char*)symb, (char*)symb);
		std::cout << symb;
	}
return os;
}
//=======================================================================
//Методы класса CyrIstream
//=======================================================================
//Шаблон для ввода типа string
template<typename _CharT, typename _Traits, typename _Alloc>
inline CyrIstream& operator >>(CyrIstream& is,
            const std::basic_string<_CharT, _Traits, _Alloc>& __str)
{
	std::string temp;
	std::cin >> temp;
	unsigned int n = temp.size();
	char *buf = new char[n+1];
	temp.copy(buf, n); buf[n] = 0;
	OemToChar(buf, (char*)buf);
	__str = std::string(buf);
	delete [] buf;
return is;
}
//=======================================================================
CyrIstream& CyrIstream::operator >>(char* s)
{
	std::string temp;
	std::cin >> temp;
	unsigned int n = temp.size();
	temp.copy(buf_, n); buf_[n] = 0;
	OemToChar(buf_, buf_);
	strncpy (s, buf_, n + 1);
return *this;
}
//=======================================================================
CyrIstream& CyrIstream::operator >>(char& c)
{
	std::cin >> buf_[0];
	buf_[1] = 0;
	OemToChar(buf_, buf_);
	c = buf_[0];
return *this;
}
//=======================================================================
CyrIstream& CyrIstream::get(char& symb)
{
	std::cin.get(buf_[0]);
	buf_[1] = 0;
	OemToChar(buf_, buf_);
	symb = buf_[0];
return *this;
}
//=======================================================================
CyrIstream& CyrIstream::get(char* pch, int nCount, char delim)
{
	std::cin.get(pch, nCount, delim);
	OemToChar(pch, pch);
return *this;
}
//=======================================================================
CyrIstream& CyrIstream::get(unsigned char* pch, int nCount, char delim)
{
	std::cin.get((char*)pch, nCount, delim);
	OemToChar((const char*)pch, (char*)pch);
return *this;
}
//=======================================================================
CyrIstream& CyrIstream::getline(char* pch, int nCount, char delim)
{
	std::cin.getline(pch, nCount, delim);
	OemToChar(pch, pch);
return *this;
}
//=======================================================================
CyrIstream& CyrIstream::read(char* pch, int nCount)
{
	std::cin.read(buf_, nCount);
	buf_[nCount] = 0;
	OemToChar(buf_, buf_);
	for(int i = 0; i < nCount; i++)
		pch[i] = buf_[i];
return *this;
}
//=======================================================================
CyrIstream& operator <<(CyrIstream &is, std::_Setprecision __f)
{
    std::cin >> __f;
    return is;
}
//======================================================================
CyrIstream& operator <<(CyrIstream &is, std::_Resetiosflags __f)
{
    std::cin >> __f;
    return is;
}
//======================================================================
CyrIstream& operator <<(CyrIstream &is, std::_Setiosflags __f)
{
    std::cin >> __f;
    return is;
}
//======================================================================
CyrIstream& operator <<(CyrIstream &is, std::_Setbase __f)
{
    std::cin >> __f;
    return is;
}
//======================================================================
CyrIstream& operator <<(CyrIstream &is, std::_Setfill<wchar_t> __f)
{
    std::cin.fill(__f._M_c);
    return is;
}
//======================================================================
CyrIstream& operator <<(CyrIstream &is, std::_Setw __f)
{
    std::cin >> __f;
    return is;
}
CyrOstream Cout;
CyrIstream Cin;
extern CyrOstream Cout;
extern CyrIstream Cin;
#endif

#ifndef RUS_H_INCLUDED_IMPLEMENTATION
	#define cout Cout
	#define cin Cin
	#define ostream CyrOstream
	#define istream CyrIstream
#endif
Ну и мб напишу для Borland'овских компиляторов.
Копируем например в rus.h код ну и вызывем так:
Код: Выделить весь код
#include "rus.h" //смотря как назвали

int main()
{
     cout << "Кириллица" << endl;
return 0;
}

-------
К величайшему сожалению "история учит нас тому, что она ничему не учит".

Это сообщение посчитали полезным следующие участники:

Отправлено: 16:41, 27-03-2009 | #64

Название темы: Кирилица в консоли