我的char数组使用Visual Studios 2017 C ++切换为318个字符

问题描述 投票:1回答:2

大家好,我有一个问题,我的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

任何帮助将不胜感激。谢谢。

c++ string visual-studio-2017 size string-length
2个回答
1
投票

std::string可能包含'\0'字符,这些字符将被NUL标识为C风格的strlen(y)终止字符串结束标记。 这就是为什么strlen()std::string::length()的结果不同。


-1
投票

如果我将字符串生成:

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那样的程序控制台)。

© www.soinside.com 2019 - 2024. All rights reserved.