字符串指针语法,这是正确的吗?

发布于 2025-01-01 03:40:17 字数 916 浏览 0 评论 0原文

我的背景是 C#,而我的 C++ 非常生疏,所以请耐心等待。

以下函数定义接受指向表示输入文件名 (szIn) 和输出文件名 (szOut) 的字符串的指针 (LPTSTR)。这是函数原型:

ConvertFile(LPTSTR szIn, LPTSTR szOut);

当前该函数是从某些 UI 代码中执行的,这是如何从指针 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"));
}

我想做一些类似的事情:

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

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

char *inputPtr;
char *outputPtr;

inputPtr = inputFilename;
outputPtr = outputFilename;

ConvertFile(inputPtr, outputPtr);

是这样的吗? Visual Studio 2008 中 Microsoft C++ 风格的正确语法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

£冰雨忧蓝° 2025-01-08 03:40:17

没有。 C++ 字符串可以完成您想要做的事情:

// 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 字符数组不能像那样连接。您必须使用 _tcscat 或其他类似的函数。

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.

标点 2025-01-08 03:40:17

这是非法的:

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

您可以使用 _tcscpy() 复制字符串:

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

_tcscpy(inputFilename, "somefile.txt");

并使用 _tcscat() 连接:

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

然后您可以传递给 ConvertFile() >:

ConvertFile(inputFilename, outputFilename);

不需要创建另一个指向这些变量的指针(因为数组将衰减为指针)。

编辑:

更改语言后,您可以使用 std::basic_string

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

并且您可以按照最初想要的方式执行串联:

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

并使用 c_str() 方法访问内部要传递给 ConvertFile() 的字符数组:

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

请注意,c_str() 返回一个 const,更改 ConvertFile()到(我假设 ConvertFile() 不会更改传入的缓冲区):

ConvertFile(LPCTSTR szIn, LPCTSTR, szOut);

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);
萌无敌 2025-01-08 03:40:17

不完全是。 TCHAR 数组应该已经衰减为 TCHAR 指针,这就是 LPTSTR(TCHAR*)。因此,您不应该使用 char* 指针,除非该函数被声明为接受 LPCSTR。长话短说,您可以直接使用 inputFilenameoutputFilename

除了这个小问题之外,真正的问题是 += 不起作用(它会尝试进行指针算术,如果有的话,这根本不是你想要的)。相反,请使用 _tcscat()

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

像往常一样,请注意以这种方式处理低级字符数组所带来的缓冲区溢出漏洞...

还有一件事:您永远不会初始化 inputFilename 和 outputFilename ——在连接它们之前,需要将它们初始化为某个字符串。

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.

比忠 2025-01-08 03:40:17

不,你不能像这样连接字符串。您正在尝试将 const char[] 附加到另一个数组的末尾...这甚至无法编译。您需要使用 _tcscat() 或(更好,因为您添加了 C++ 标签), std::basic_string

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>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文