std::string equivalent for data with null characters?

前端 未结 5 1118
梦谈多话
梦谈多话 2021-01-13 18:57

I\'d like to read a binary file and use something like std::string that automatically resizes the buffer and such.

I\'m using Visual C++. What are my op

相关标签:
5条回答
  • 2021-01-13 19:37

    std::string should be safe to do so... you only have to be careful using .c_str() method. Use .data().

    0 讨论(0)
  • 2021-01-13 19:40

    std::string allows NUL characters so you can just go ahead and use that.

    There is no problem with using c_str() or data(). Yes, the embedded NULs will be in your data, but if you don't use them to terminate your strings (you'll need to call length() to find out how many bytes are in your string) then all will be good.

    0 讨论(0)
  • 2021-01-13 19:43

    The std::string class already does handle data with embedded NUL characters. What problem are you encountering?

    Note that when using the .c_str() method, any embedded NUL character will terminate the returned C-style string.

    0 讨论(0)
  • 2021-01-13 19:52

    Yes you can have embedded nulls in your std::string.

    Example:

    std::string s;
    s.push_back('\0');
    s.push_back('a');
    assert(s.length() == 2);
    

    Note: std::string's c_str() member will always append a null character to the returned char buffer; However, std::string's data() member may or may not append a null character to the returned char buffer.

    Be careful of operator+=

    One thing to look out for is to not use operator+= with a char* on the RHS. It will only add up until the null character.

    For example:

    std::string s = "hello";
    s += "\0world";
    assert(s.length() == 5);
    

    The correct way:

    std::string s = "hello";
    s += std::string("\0world", 6);
    assert(s.length() == 11);
    

    Storing binary data more common to use std::vector

    Generally it's more common to use std::vector to store arbitrary binary data.

    std::vector<char> buf;
    buf.resize(1024);
    char *p = &buf.front();
    

    It is probably more common since std::string's data() and c_str() members return const pointers so the memory is not modifiable. with &buf.front() you are free to modify the contents of the buffer directly.

    0 讨论(0)
  • 2021-01-13 19:54

    You can always use std::vector<unsigned char> v (or whatever type of input you expect) and then to get at the buffer you simply &v[0] and v.size() for the size.

    0 讨论(0)
提交回复
热议问题