如何使std :: getline处理硬编码字符串中的换行符之类的换行符?
我有这段代码:
std::string sServerRequest;
std::getline(std::cin, sServerRequest);
std::cout << sServerRequest;
如果我在控制台中输入此输入:
-----BEGIN CERTIFICATE REQUEST-----\nMIICqjCCAZICAQAwZTELMAkGA1UEBhMCVVMxDjAMBgNVBAgMBVRleGFzMRAwDgYD\nVQQHDAdIb3VzdG9uMQwwCgYDVQQKDANEaXMxJjAkBgNVBAMMHVNWUi5DTTAwNDA5\nRDk4OEU5NS4xNTE3NTE1NzQzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC\nAQEAq9inWXxEngc3lshyMWsHQuEcKtPbYK5TbEFDU7fvwmNlYo/Inu+K5iEWwYSV\nC1ZM+HyZ/4yneD8oZe9ojRYSjvgSZc+Ag+SsEmBlxmk6n7QgE1qLZi7K65YAk8lc\nkLtxGhocjokkcxgv7nnxtTxJXP88z44w0+S0EjieeDCbwjEpwzwgCv3rzbW2fpN0\nSw+KAaXISGN66PvTFh386kXZM5J3HOFDl7QykJQQMjvutCKkrCXoomn25Gd8RiOb\ntfLd7OsP4b2UuFJBDLRxHqqB4swrIsBSHnB35Yw8YvN//ywjw11hLKQXY8eTBbtJ\nszQzITd0+Jr4MMTLkG9oYOScuQIDAQABoAAwDQYJKoZIhvcNAQELBQADggEBAAI0\n7dbOMjiyrJC5OR+xHfTix1Scdwm3HH+sgfSuT02kcbGdpgm54YRq4QVEGhrGJ1jy\n6GpB8YkPMkg1MtC3l6TFbhY0S89B/IRXSgM9tJXrLYcfpHMtp9TjOQgh6QlQ/ny1\nms9ABf/9pGnYsAgsD/z9dOVlY6UYugSfcF2r4QIJqpedawIXJ1hjTtm6JQW9d1U3\nkaTSJv0qJdsIXDcWFJuNDO4TBblmZZWc4ZEfqIKbe5vtsmwDTKSEOi4LzKLV9lH7\nBjA4eFJ5XEgyuzy0/c9+Cf+55jWaD5WckcVrV9YlQR4xt/c5kEyWFAZF+Hg5kGTp\ncfN3bGu1AsZGtey3CBY=\n-----END CERTIFICATE REQUEST-----\n
这是控制台窗口中的输出(不适合我的应用程序):
-----BEGIN CERTIFICATE REQUEST-----\nMIICqjCCAZICAQAwZTELMAkGA1UEBhMCVVMxDjAMBgNVBAgMBVRleGFzMRAwDgYD\nVQQHDAdIb3VzdG9uMQwwCgYDVQQKDANEaXMxJjAkBgNVBAMMHVNWUi5DTTAwNDA5\nRDk4OEU5NS4xNTE3NTE1NzQzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC\nAQEAq9inWXxEngc3lshyMWsHQuEcKtPbYK5TbEFDU7fvwmNlYo/Inu+K5iEWwYSV\nC1ZM+HyZ/4yneD8oZe9ojRYSjvgSZc+Ag+SsEmBlxmk6n7QgE1qLZi7K65YAk8lc\nkLtxGhocjokkcxgv7nnxtTxJXP88z44w0+S0EjieeDCbwjEpwzwgCv3rzbW2fpN0\nSw+KAaXISGN66PvTFh386kXZM5J3HOFDl7QykJQQMjvutCKkrCXoomn25Gd8RiOb\ntfLd7OsP4b2UuFJBDLRxHqqB4swrIsBSHnB35Yw8YvN//ywjw11hLKQXY8eTBbtJ\nszQzITd0+Jr4MMTLkG9oYOScuQIDAQABoAAwDQYJKoZIhvcNAQELBQADggEBAAI0\n7dbOMjiyrJC5OR+xHfTix1Scdwm3HH+sgfSuT02kcbGdpgm54YRq4QVEGhrGJ1jy\n6GpB8YkPMkg1MtC3l6TFbhY0S89B/IRXSgM9tJXrLYcfpHMtp9TjOQgh6QlQ/ny1\nms9ABf/9pGnYsAgsD/z9dOVlY6UYugSfcF2r4QIJqpedawIXJ1hjTtm6JQW9d1U3\nkaTSJv0qJdsIXDcWFJuNDO4TBblmZZWc4ZEfqIKbe5vtsmwDTKSEOi4LzKLV9lH7\nBjA4eFJ5XEgyuzy0/c9+Cf+55jWaD5WckcVrV9YlQR4xt/c5kEyWFAZF+Hg5kGTp\ncfN3bGu1AsZGtey3CBY=\n-----END CERTIFICATE REQUEST-----\n
此代码:
std::string sServerRequest= "-----BEGIN CERTIFICATE REQUEST-----\nMIICqjCCAZICAQAwZTELMAkGA1UEBhMCVVMxDjAMBgNVBAgMBVRleGFzMRAwDgYD\nVQQHDAdIb3VzdG9uMQwwCgYDVQQKDANEaXMxJjAkBgNVBAMMHVNWUi5DTTAwNDA5\nRDk4OEU5NS4xNTE3NTE1NzQzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC\nAQEAq9inWXxEngc3lshyMWsHQuEcKtPbYK5TbEFDU7fvwmNlYo/Inu+K5iEWwYSV\nC1ZM+HyZ/4yneD8oZe9ojRYSjvgSZc+Ag+SsEmBlxmk6n7QgE1qLZi7K65YAk8lc\nkLtxGhocjokkcxgv7nnxtTxJXP88z44w0+S0EjieeDCbwjEpwzwgCv3rzbW2fpN0\nSw+KAaXISGN66PvTFh386kXZM5J3HOFDl7QykJQQMjvutCKkrCXoomn25Gd8RiOb\ntfLd7OsP4b2UuFJBDLRxHqqB4swrIsBSHnB35Yw8YvN//ywjw11hLKQXY8eTBbtJ\nszQzITd0+Jr4MMTLkG9oYOScuQIDAQABoAAwDQYJKoZIhvcNAQELBQADggEBAAI0\n7dbOMjiyrJC5OR+xHfTix1Scdwm3HH+sgfSuT02kcbGdpgm54YRq4QVEGhrGJ1jy\n6GpB8YkPMkg1MtC3l6TFbhY0S89B/IRXSgM9tJXrLYcfpHMtp9TjOQgh6QlQ/ny1\nms9ABf/9pGnYsAgsD/z9dOVlY6UYugSfcF2r4QIJqpedawIXJ1hjTtm6JQW9d1U3\nkaTSJv0qJdsIXDcWFJuNDO4TBblmZZWc4ZEfqIKbe5vtsmwDTKSEOi4LzKLV9lH7\nBjA4eFJ5XEgyuzy0/c9+Cf+55jWaD5WckcVrV9YlQR4xt/c5kEyWFAZF+Hg5kGTp\ncfN3bGu1AsZGtey3CBY=\n-----END CERTIFICATE REQUEST-----\n";
std::cout << "Specify Server Request" << std::endl;
std::cout << sServerRequest;
在控制台窗口中生成此结果(可取):
Specify Server Request
-----BEGIN CERTIFICATE REQUEST-----
MIICqjCCAZICAQAwZTELMAkGA1UEBhMCVVMxDjAMBgNVBAgMBVRleGFzMRAwDgYD
VQQHDAdIb3VzdG9uMQwwCgYDVQQKDANEaXMxJjAkBgNVBAMMHVNWUi5DTTAwNDA5
RDk4OEU5NS4xNTE3NTE1NzQzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
AQEAq9inWXxEngc3lshyMWsHQuEcKtPbYK5TbEFDU7fvwmNlYo/Inu+K5iEWwYSV
C1ZM+HyZ/4yneD8oZe9ojRYSjvgSZc+Ag+SsEmBlxmk6n7QgE1qLZi7K65YAk8lc
kLtxGhocjokkcxgv7nnxtTxJXP88z44w0+S0EjieeDCbwjEpwzwgCv3rzbW2fpN0
Sw+KAaXISGN66PvTFh386kXZM5J3HOFDl7QykJQQMjvutCKkrCXoomn25Gd8RiOb
tfLd7OsP4b2UuFJBDLRxHqqB4swrIsBSHnB35Yw8YvN//ywjw11hLKQXY8eTBbtJ
szQzITd0+Jr4MMTLkG9oYOScuQIDAQABoAAwDQYJKoZIhvcNAQELBQADggEBAAI0
7dbOMjiyrJC5OR+xHfTix1Scdwm3HH+sgfSuT02kcbGdpgm54YRq4QVEGhrGJ1jy
6GpB8YkPMkg1MtC3l6TFbhY0S89B/IRXSgM9tJXrLYcfpHMtp9TjOQgh6QlQ/ny1
ms9ABf/9pGnYsAgsD/z9dOVlY6UYugSfcF2r4QIJqpedawIXJ1hjTtm6JQW9d1U3
kaTSJv0qJdsIXDcWFJuNDO4TBblmZZWc4ZEfqIKbe5vtsmwDTKSEOi4LzKLV9lH7
BjA4eFJ5XEgyuzy0/c9+Cf+55jWaD5WckcVrV9YlQR4xt/c5kEyWFAZF+Hg5kGTp
cfN3bGu1AsZGtey3CBY=
-----END CERTIFICATE REQUEST-----
^如何更改逻辑以处理硬编码字符串等新行?是否有一些我需要删除的逃脱?
处理sServerRequest内容的逻辑要求字符串的格式采用第二个示例的格式(在此问题中标记为可取)。字符的打印并不是那么重要。重要的是sServerRequest中的内容。我处理它的逻辑可以处理第二种情况,但不能处理第一种情况。
答案 0 :(得分:0)
这有理想的结果:
std::string sServerRequest;
std::cout << "Specify Server Request" << std::endl;
std::getline(std::cin, sServerRequest);
size_t f;
do
{
f = sServerRequest.find(
"\\n"
);
if (f != std::string::npos)
{
sServerRequest.replace(
f,
std::string("\\n").length(),
"\n"
);
}
} while (f != std::string::npos);
std::cout << sServerRequest;
感谢。