views:

59

answers:

1

A C++ beginner's question. Here is what I have currently:

// From tchar.h
#define _T(x)       __T(x)

...

// From tchar.h
#define __T(x)      L ## x

...

// In MySampleCode.h
#ifdef _UNICODE
    #define tcout wcout
#else
    #define tcout cout
#endif

...

// In MySampleCode.cpp
CAtlString strFileName;
if (bIsInteractiveMode)
{
char* cFileName = new char[513];
tcout << endl;
tcout << _T("Enter the path to a file that you would like to XYZ(purpose obfuscated) ") << endl;
tcout << _T(">>> ");            
cin.getline(cFileName, 512);
strFileName = cXmlFileName;
}

// Demonstrates how CAtlString can be printed using `tcout`.
tcout << _T("File named '") << strFileName.GetString() << _T("' does not exist.") << endl;

This happens to "work" in the US, but I have no idea what will happen if ... say a French user is running this app and starts to enter weird characters such as Çanemeplaîtpas.xml on command line. I am looking for a clean way to populate a string of the CAtlString type. The maximum length of the input can always be set long enough, but ideally I would like to limit the unicode, and non-unicode entries to the same number of characters. Hopefully doing so is reasonably easy and elegant.

+2  A: 

Shouldn't you be using wcin stream if you expect unicode input?

#include <iostream>
#include <string>
#include <locale>

int main()
{
    using namespace std;

    std::locale::global(locale("en_US.utf8"));

    std::wstring s;

    std::wcin >> s;

    std::wcout << s;

}

m1tk4
Please provide a somewhat complete code sample - I would love that!
Hamish Grubijan
I edited the reply above.
m1tk4