Jump to content
  • Advertisement
Sign in to follow this  
jeff8j

C++ std::string null characters yes or no?

This topic is 2589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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;

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!