Is there a format specifier that always means a char string with _tprintf?

When you create an application on Windows with support TCHAR, %sin _tprintf()means a line char *for Ansi lines and wchar_t *for Unicode assemblies, but it %smeans the opposite.

But are there any format specifiers that always mean a string char *, whether it is an Ansi or Unicode assembly? Since even in Windows UTF-16 is not actually used for files or networks, it turns out that it is still quite common that you need to deal with byte strings, regardless of the type of native character that you are compiling as an application.

+7
source share
4 answers

The modifier hforces both %s, and %son char*, and the modifier lforces both wchar_t*, i.e. %hs, %hs, %ls, And %ls.

+5
source

This may also solve your problem:

_TCHAR *message;
_tprintf(_T("\n>>>>>> %d") TEXT(" message is:%s\n"),4,message);
+1
source

- :

#ifdef _UNICODE
#define PF_ASCIISTR    "%S"L
#define PF_UNICODESTR  "%s"L
#else
#define PF_ASCIISTR    "%s"
#define PF_UNICODESTR  "%S"
#endif

PF_ASCIISTR PF_UNICODESTR , C:

_tprintf(_T("There are %d ") PF_ASCIISTR _T(" over the table"), 10, "pens");
0

, _vsntprintf_s "% s" TCHAR GCC, MSVC. , :

int myprintf(const TCHAR* lpszFormat, va_list argptr) {
    int len = _vsctprintf(lpszFormat, argptr); // -1:err
    if (len<=0) {return len;}
    auto* pT = new TCHAR[2 + size_t(len)];
    _vsntprintf_s(pT, (2+len)*sizeof(TCHAR), 1+len, lpszFormat, argptr);
    int rv = printf("%ls", pT);
    delete[] pT;
    return rv;
}
int myprintf(const TCHAR* lpszFormat, ...) {
    va_list argptr;
    va_start(argptr, lpszFormat);
    int rv = myprintf(lpszFormat, argptr);
    va_end(argptr);
    return rv;
}



int main(int, char**) { return myprintf(_T("%s"), _T("Test")); }
0

All Articles