字符串指针语法,这是正确的吗?
我的背景是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有。 C++ 字符串可以完成您想要做的事情:
C 字符数组不能像那样连接。您必须使用
_tcscat
或其他类似的函数。Nope. C++ strings can do what you're trying to do:
C char arrays can't be concatenated like that. You'd have to use
_tcscat
or other similar functions.这是非法的:
您可以使用
_tcscpy()
复制字符串:并使用
_tcscat()
连接:然后您可以传递给
ConvertFile()
>:不需要创建另一个指向这些变量的指针(因为数组将衰减为指针)。
编辑:
更改语言后,您可以使用
std::basic_string
:并且您可以按照最初想要的方式执行串联:
并使用
c_str()
方法访问内部要传递给ConvertFile()
的字符数组:请注意,
c_str()
返回一个const
,更改ConvertFile()
到(我假设ConvertFile()
不会更改传入的缓冲区):This is illegal:
You can use
_tcscpy()
to copy strings:and use
_tcscat()
to concatenate:You can then pass to
ConvertFile()
: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
:And you can perform concatenation as you initially wanted:
And use
c_str()
method to access the internal character array to pass toConvertFile()
:Note that
c_str()
returns aconst
, changeConvertFile()
to (I making an assumption thatConvertFile()
does not change the buffers passed in):不完全是。 TCHAR 数组应该已经衰减为 TCHAR 指针,这就是 LPTSTR(
TCHAR*
)。因此,您不应该使用 char* 指针,除非该函数被声明为接受 LPCSTR。长话短说,您可以直接使用inputFilename
和outputFilename
。除了这个小问题之外,真正的问题是
+=
不起作用(它会尝试进行指针算术,如果有的话,这根本不是你想要的)。相反,请使用_tcscat()
:像往常一样,请注意以这种方式处理低级字符数组所带来的缓冲区溢出漏洞...
还有一件事:您永远不会初始化
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 usingchar*
pointers, unless the function is declared to accept LPCSTR. Long story short, you can useinputFilename
andoutputFilename
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()
: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
andoutputFilename
-- They'll need to be initialized to some string before you can concatenate to them.不,你不能像这样连接字符串。您正在尝试将 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>
.