我的 char 数组使用 Visual Studios 2017 C++ 在 318 个字符处截断
My char arrays cut off at 318 character using Visual Studios 2017 C++
大家好 我有一个问题,我的 char arrays (strings)
不会显示任何超过 318 characters
的内容。如果我执行 std::string
没问题并显示整个内容...但是如果我尝试使用 std::string.c_str()
将其转换为字符数组,它只会再次生成第一个 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 *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
可能包含 '[=11=]'
个字符,这些字符将被 strlen(y)
.
识别为 C 风格 NUL
终止的字符串结束标记
这就是 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', '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 那样的程序控制台)。
大家好 我有一个问题,我的 char arrays (strings)
不会显示任何超过 318 characters
的内容。如果我执行 std::string
没问题并显示整个内容...但是如果我尝试使用 std::string.c_str()
将其转换为字符数组,它只会再次生成第一个 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 *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
可能包含 '[=11=]'
个字符,这些字符将被 strlen(y)
.
识别为 C 风格 NUL
终止的字符串结束标记
这就是 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', '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 那样的程序控制台)。