大家好,我有一个问题,我的char arrays (strings)
将不会显示任何超过318 characters
。如果我做一个std::string
它很好,并显示整个事情......但如果我尝试使用std::string.c_str()
将其转换为char数组,它只会再生成第一个318 characters
。任何帮助将不胜感激。我正在使用Visual Studio 2017
。实际的字符串长度是517chars
。
例...
std::string x = "INSERT INTO oms.Customers (customerFirstName, CustomerMiddleInitial, customerLastName, customerPrefixSuffix, customerCompanyName, customerStreetReference, customerStreetAddress, customerCity, customerState, customerZip, CountryID, customerPhoneNumber, customerEmail, customerIsCompany, customerIsActive) Values('Bill', '
INSERT INTO oms.Customers(customerFirstName, CustomerMiddleInitial, customerLastName, customerPrefixSuffix, customerCompanyName, customerStreetReference, customerStreetAddress, customerCity, customerState, customerZip, CountryID, customerPhoneNumber, customerEmail, customerIsCompany, customerIsActive)Values('Bill', ' ', 'Cosby', '', 'The Cosby Show', 'Studio A', '1818 broadway st', 'Philadelphia', 'PA', '19809', '', '6169875942', '[email protected]', '', ');SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY];"
但如果我制作任何类型的char数组,如果我做了
char *y;
y= x.c_str
只有下面会显示
INSERT INTO oms.Customers (customerFirstName, CustomerMiddleInitial, customerLastName, customerPrefixSuffix, customerCompanyName, customerStreetReference, customerStreetAddress, customerCity, customerState, customerZip, CountryID, customerPhoneNumber, customerEmail, customerIsCompany, customerIsActive) Values('Bill', '
如果我做strlen(y)
我得到318
。如果我做x.length()
我得到517
。
任何帮助将不胜感激。谢谢。
std::string
可能包含'\0'
字符,这些字符将被NUL
标识为C风格的strlen(y)
终止字符串结束标记。
这就是为什么strlen()
和std::string::length()
的结果不同。
如果我将字符串生成:
std::string x = R"(INSERT INTO oms.Customers (customerFirstName, CustomerMiddleInitial, customerLastName, customerPrefixSuffix, customerCompanyName, customerStreetReference,
customerStreetAddress, customerCity, customerState, customerZip, CountryID, customerPhoneNumber, customerEmail, customerIsCompany, customerIsActive) Values('Bill',
'
INSERT INTO oms.Customers(customerFirstName, CustomerMiddleInitial, customerLastName, customerPrefixSuffix, customerCompanyName, customerStreetReference,
customerStreetAddress, customerCity, customerState, customerZip, CountryID, customerPhoneNumber, customerEmail, customerIsCompany, customerIsActive)Values('Bill',
' ', 'Cosby', '', 'The Cosby Show', 'Studio A', '1818 broadway st', 'Philadelphia', 'PA', '19809', '', '6169875942', '[email protected]', '', ');SELECT
SCOPE_IDENTITY() AS[SCOPE_IDENTITY]; )";
char const *pStr = x.c_str();
_RPTA("strlen: %d\r\n", strlen(pStr));
长度打印为857(_RPTA是一个自定义宏,打印到VS调试跟踪而不是像printf那样的程序控制台)。