Sign in to follow this  
nstar

Codepage 437, access from within a C++ application?

Recommended Posts

nstar    122
Hello, I wish to change the codepage from 850 (on most systems) to 437, which is the cp that I need for my app. I know that on an XP system I am able to use the console command 'chcp 437', but I don't want to call that command from my app, because there's a risk th't that may not work on some systems. Any ideas?

Share this post


Link to post
Share on other sites
Anon Mike    1098
setlocale is the normal way of doing this sort of thing in C++. What exactly are you trying to do though? It's very unlikely that simply calling that will solve all your problems whatever they are.

Share this post


Link to post
Share on other sites
Modena_au    172
Mmm... you may want to look at it converting your app to UNICODE if you need to access characters outside of the standard code page. If you are, however, trying to work with files written using a different code page, I'd personally convert them over to Unicode. Multiple code pages were a hack to begin with and should be avoided if at all possible. With a little more info though, we might be able to give some more solid advice.

Share this post


Link to post
Share on other sites
nstar    122
Read carefully...

I don't want to >expect< a user of my app to type the following before they run the app:

chcp 437

...becuase ecpecting people to do things like that isn't the best practice, since people are very unreliable generally!

So, instead I propose to change the DOS box codepage for them!

I just need to run that command 'mode con cp prepare 437 blahblah' from within my app; however, there is a risk that ega.cpi will not be present on the operating system on which a user of my app is running my app, so I must include ega.cpi with my program, and load it from within my program. Don't worry about any problems you may predict, I just need to know how to load the code page file ega.cpi with codepage 437 (437 is the UK codepage, whereas 850 is the default USA codepage).

Share this post


Link to post
Share on other sites
tychon    652
The command you're looking for is SetConsoleOutputCP(), defined under windows.h.

Just an example, but:
#include <windows.h>

#include <iostream>
#include <string>
#include <vector>

typedef std::vector< std::string > strings;

int EnumerateCodepages( strings& list )
{
HKEY hCodepage = NULL;
HKEY hKey = HKEY_LOCAL_MACHINE;
std::string sSubKey = "System\\CurrentControlSet\\Control\\Nls\\Codepage";

if ( RegOpenKeyEx( hKey, sSubKey.c_str(), NULL, KEY_READ, &hCodepage ) != ERROR_SUCCESS )
return 0;

char keyValue[256];
DWORD keySize = 256;
for ( DWORD i = 0; RegEnumValue( hCodepage, i, keyValue, &keySize, NULL, NULL, NULL, NULL ) == ERROR_SUCCESS; ++i, keySize = 256 )
list.push_back( std::string( keyValue ) );

RegCloseKey( hCodepage );

return list.size();
}

int main()
{
strings cp;

if ( EnumerateCodepages( cp ) == 0 )
{
std::cout << "Unable to enumerate codepages.\n";
return 1;
}

std::cout << "AVAILABLE CODEPAGES\n";
for ( strings::iterator i = cp.begin(); i != cp.end(); ++i )
std::cout << "\t" << *i << "\n";
std::cout << std::endl;

int to;
std::cout << "Please select a codepage: ";
std::cin >> to;

if ( SetConsoleOutputCP( to ) == 0 )
{
std::cout << "Failure or invalid codepage, exiting.\n";
return 1;
}

std::cout << "Welcome to your new codepage!\n";

return 0;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this