4

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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147
  • Here is a similar question: http://stackoverflow.com/questions/3207704 – Philipp Jul 12 '10 at 16:45
  • True, thanks. The answers refer to `std::wstring` and `std::string` however. I would like to use an ATL string. – Hamish Grubijan Jul 12 '10 at 16:47
  • unrelated to the question, i think you should define yout cFileName like this: char* cFileName = new char[MAX_PATH]; since it is a path, you should have MAX_PATH imo – jeyejow Jun 16 '17 at 08:12

1 Answers1

5

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
  • 3,439
  • 1
  • 22
  • 27