In C/C++, strings are NULL terminated.
Could I use stringstream
as a memory stream like MemoryStream
in C#?
Data of memory streams may have \0
values in the middle of data, but C++ strings are NULL terminated.
In C/C++, strings are NULL terminated.
Could I use stringstream
as a memory stream like MemoryStream
in C#?
Data of memory streams may have \0
values in the middle of data, but C++ strings are NULL terminated.
When storing character sequences in a std::string
you can have included null characters. Correspondingly, a std::stringstream
can deal with embedded null characters as well. However, the various formatted operations on streams won't pass through the null characters. Also, when using a built-in string to assign values to a std::string
the null characters will matter, i.e., you'd need to use the various overloads taking the size of the character sequence as argument.
What exactly are you trying to achieve? There may be an easier approach than traveling in string streams. For example, if you want to read the stream interface to interact with a memory buffer, a custom stream buffer is really easy to write and setup:
struct membuf
: std::streambuf
{
membuf(char* base, std::size_t size) {
this->setp(base, base + size);
this->setg(base, base, base + size);
}
std::size_t written() const { return this->pptr() - this->pbase(); }
std::size_t read() const { return this->gptr() - this->eback(); }
};
int main() {
// obtain a buffer starting at base with size size
membuf sbuf(base, size);
std::ostream out(&sbuf);
out.write("1\08\09\0", 6); // write three digits and three null chars
}
In C/C++, strings are NULL terminated.
Each language has their own string features.
Could I use stringstream as a memory stream like MemoryStream in C#?
Sure. The Memory stream is a stream that is backed by memory (rather than a file). This is exactly what std::stringstream is. There are some differences in the interface but these are minor and use of the documentation should easily resolve any confusion.
Data of memory streams may have \0 values in the middle of data, but C++ strings are NULL terminated.
This is totally incorrect.
C and C++ are two different languages, in C sequence of continues character means string, and you can treat it as a memory part which can set/get its values, using memcpy()
,memset()
,memcmp()
.
In C++ strings means a class of information which used to get correct data as string. So you can't treat it as a sequence of memory location with char
type.