你好,我有一个问题,我的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', 'Bill.Cosby@ididntdoit.com', '', ');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。
任何帮助都将不胜感激。谢谢。
发布于 2017-12-26 07:53:52
std::string可能包含'\0'字符,这些字符将被strlen(y)标识为C样式NUL终止字符串结束标记。
这就是为什么strlen()和std::string::length()的结果不同的原因。
发布于 2017-12-26 07:56:07
如果我把绳子弄成原样:
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', 'Bill.Cosby@ididntdoit.com', '', ');SELECT
SCOPE_IDENTITY() AS[SCOPE_IDENTITY]; )";
char const *pStr = x.c_str();
_RPTA("strlen: %d\r\n", strlen(pStr));长度打印为857 (_RPTA是一个自定义宏,它输出到VS调试跟踪,而不是像printf那样的程序控制台)。
https://stackoverflow.com/questions/47975310
复制相似问题