String pointer syntax, is this correct?

762 Views Asked by At

I'm coming from a C# background and my C++ is very rusty so please bear with me.

The following function definition accepts pointers (LPTSTR) to strings representing an input filename (szIn) and output filename (szOut). Here is the function prototype:

ConvertFile(LPTSTR szIn, LPTSTR szOut);

Currently the function is executed from within some UI code, here's an example of how the output filename is obtained from the pointer szOutput:

TCHAR szOutput[255];    // output filename

if (g_szFilename[0] != 0)
{
    SetDlgItemText(hMainDlg, IDC_EDIT_INPUT, g_szFilename);
    _tcscpy(szOutput, g_szFilename);
    TCHAR * szExt = _tcsrchr(szOutput, '.');
    if (szExt != NULL) *szExt = 0;
    _tcscat(szOutput, _T(".png"));
}

I want to do something along these lines:

TCHAR inputFilename[256];
TCHAR outputFilename[256];

inputFilename += "somefile.txt";
outputFilename += "someotherfile.txt";

char *inputPtr;
char *outputPtr;

inputPtr = inputFilename;
outputPtr = outputFilename;

ConvertFile(inputPtr, outputPtr);

Is this the correct syntax for Microsoft's flavor of C++ in Visual Studio 2008?

4

There are 4 best solutions below

9
On

No, you cannot concatenate a string like that. You are trying to append a const char[] onto the end of another array... which won't even compile. You need to use _tcscat() or (better yet as you have added the C++ tag), std::basic_string<TCHAR>.

1
On

Not quite. The TCHAR arrays should already decay into TCHAR pointers, which is what LPTSTR is (a TCHAR*). So, you shouldn't be using char* pointers, unless the function is declared to accept LPCSTR. Long story short, you can use inputFilename and outputFilename directly.

Aside from that minor matter, the real issue is that += won't work (it will attempt to do pointer arithmetic, if anything, which is not at all what you want). Instead, use _tcscat():

_tcscat(inputFilename, TEXT("somefile.txt"));
// ...

As usual, beware the buffer overflow vulnerabilities that come with working with low-level character arrays this way...

One more thing: You never initialize inputFilename and outputFilename -- They'll need to be initialized to some string before you can concatenate to them.

0
On

This is illegal:

inputFilename += "somefile.txt";
outputFilename += "someotherfile.txt";

You can use _tcscpy() to copy strings:

TCHAR inputFilename[256]  = { 0 }; /* FYI, the '= { 0 }' initialises the array.*/
TCHAR outputFilename[256] = { 0 };

_tcscpy(inputFilename, "somefile.txt");

and use _tcscat() to concatenate:

_tcscat(inputFilename, "another-bit.txt");

You can then pass to ConvertFile():

ConvertFile(inputFilename, outputFilename);

there is no need to create another pointer to these variables (as the arrays will decay to pointers).

EDIT:

After change in language you can make use of std::basic_string:

std::basic_string<TCHAR> inputFilename(TEXT("somefile.txt"));
std::basic_string<TCHAR> outputFilename(TEXT("someotherfile.txt"));

And you can perform concatenation as you initially wanted:

inputFilename += TEXT("a-bit-more");

And use c_str() method to access the internal character array to pass to ConvertFile():

ConvertFile(inputFilename.c_str(), outputFilename.c_str());

Note that c_str() returns a const, change ConvertFile() to (I making an assumption that ConvertFile() does not change the buffers passed in):

ConvertFile(LPCTSTR szIn, LPCTSTR, szOut);
3
On

Nope. C++ strings can do what you're trying to do:

// Since we don't know whether to use string or wstring, we're stuck
// with basic_string<TCHAR>
std::basic_string<TCHAR> inputFilename, outputFilename;

// Note the TEXT macro...it makes sure your chars are TCHARs
inputFilename += TEXT("somefile.txt");
outputFilename += TEXT("someotherfile.txt");

// basic_string::c_str() returns you a pointer suitable for C stuff
// (since this is a string of TCHARs, we'll get a const TCHAR* back)
ConvertFile(inputFilename.c_str(), outputFilename.c_str());

C char arrays can't be concatenated like that. You'd have to use _tcscat or other similar functions.