Just a simple question can std::string contain null characters?
I was using vector<char> for holding file data because it can have null characters. Can I switch it to a string and save alot of effort?
C++ std::string null characters yes or no?
Yeah, technically an std::string can store a null character ('\0'), but you should be a little careful about doing it. std::string is not null terminated, so it doesn't really care if there's a '\0' somewhere in it or not. However, let me just illustrate some things you need to watch out for:
// Won't contain "world!" because the constructor will set it to the C-style string, which ends at the '\0'
std::string str("Hello\0world!");
// Won't get "string" appended to it because it's trying to append the C-style string, which ends at the '\0'
str += "some\0string";
// There we go, now we've got '\0' in there properly!
str.push_back('\0');
// Works just fine, the string after this point is "Hellosome\0this text is after the null character"
str += "this text is after the null character";
// Any guesses on what the output will be?
std::cout << "Length: " << str.length() << "\nText: " << str;
Yeah, technically an std::string can store a null character ('\0'), but you should be a little careful about doing it. std::string is not null terminated, so it doesn't really care if there's a '\0' somewhere in it or not. However, let me just illustrate some things you need to watch out for:
// Won't contain "world!" because the constructor will set it to the C-style string, which ends at the '\0'
std::string str("Hello\0world!");
// Won't get "string" appended to it because it's trying to append the C-style string, which ends at the '\0'
str += "some\0string";
// There we go, now we've got '\0' in there properly!
str.push_back('\0');
// Works just fine, the string after this point is "Hellosome\0this text is after the null character"
str += "this text is after the null character";
// Any guesses on what the output will be?
std::cout << "Length: " << str.length() << "\nText: " << str;
ok I see I cant put a null character into it directly thats fine I dont have any plans to.
Whats wrong with the str.length()? I got 47 length coping what you have there?
I tried this
str = "1";
str.push_back('\0');
str += "234";
and str.length() returned 5 thats what I was expecting
Thank You
Yeah, exactly. It wasn't really a trick question. I was trying to illustrate the fact that std::string can contain null characters, and it will count them, and it will output "Hellosome this text is after the null character" (though it makes it look like there's a space rather than a null character). However, you can't try to add raw C-style strings like you normally would because as soon as it sees a '\0' character it's going to think that's the end of the C-style string. So you have to use push_back('\0') or a few other methods to get a null character in there. But putting a '\0' in the std::string is perfectly well defined, you just have to make sure you don't screw up trying to do it
The problem comes in printing or c style strings. If you cout << str. It hits the first null and stops, hence null terminated. You give it a pointer to the first part of the string and it will print up till it terminates with a null.
The problem comes in printing or c style strings. If you cout << str. It hits the first null and stops, hence null terminated. You give it a pointer to the first part of the string and it will print up till it terminates with a null.
The stream objects are perfectly capable of outputting string objects containing null characters. On the other hand, cout << str.c_str() won't print the whole string, since C-string are null terminated.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement