string - How have null value inside basic_string in c++ -
is there way have , process null value inside std::basic_string?
sometimes string sequence being passed has null values. example below code outputs 1234,5678 instead of whole string.
#include <iostream> #include <string> #include <cstring> int main () { int length; std::string str = "1234,5678\000,abcd,efg#"; std::cout << "length"<<str.c_str(); } i need complete string.
first, you'll have tell string constructor size; can determine size if input null-terminated. work:
std::string str("1234,5678\000,abcd,efg#", sizeof("1234,5678\000,abcd,efg#")-1); there's no particularly nice way avoid duplication; declare local array
char c_str[] = "1234,5678\000,abcd,efg#"; // in c++11, perhaps 'auto const & c_str = "...";' std::string str(c_str, sizeof(c_str)-1); which might have run-time cost; or use macro; or build string in pieces
std::string str = "1234,5678"; str += '\0'; str += ",abcd,efg#"; finally, stream string (for size known) rather extracting c-string pointer (for size determined looking null terminator):
std::cout << "length" << str; update: pointed out in comments, c++14 adds suffix basic_string literals:
std::string str = "1234,5678\000,abcd,efg#"s; ^ which, reading of draft standard, should work if there embedded null character.
Comments
Post a Comment