Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could I use stringstream as a memory stream like `MemoryStream` in C#?

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.

like image 392
Amir Saniyan Avatar asked Nov 24 '12 15:11

Amir Saniyan


People also ask

Can we use Stringstream in C?

StringStream in C++ is similar to cin and cout streams and allows us to work with strings. Like other streams, we can perform read, write, and clear operations on a StringStream object. The standard methods used to perform these operations are defined in the StringStream class.

What is the difference between stream and MemoryStream?

You would use the FileStream to read/write a file but a MemoryStream to read/write in-memory data, such as a byte array decoded from a string. You would not use a Stream in and of itself, but rather use it for polymorphism, i.e. passing it to methods that can accept any implementation of Stream as an argument.

Can we convert string to stream?

We can convert a String to an InputStream object by using the ByteArrayInputStream class. The ByteArrayInputStream is a subclass present in InputStream class.

What is the difference between Stringstream and string?

Very Informally: A string is a collection of characters, a stream is a tool to manipulate moving data around. A string stream is a c++ class that lets you use a string as the source and destination of data for a stream.


2 Answers

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
}
like image 122
Dietmar Kühl Avatar answered Oct 25 '22 08:10

Dietmar Kühl


In C/C++, strings are NULL terminated.

  • Two completely different languages (that have some commonality in syntax). But C is about as close to C++ as Jave is to C# (in that they are different).

Each language has their own string features.

  • C uses a sequence of bytes that by convention is terminated by a '\0' byte.
    • Commonly referred to as a C-String.
    • There is nothing special about this area of memory it is just a sequence of bytes.
    • There is no enforcement of convention and no standard way to build.
    • It is a huge area of bugs in C programs.
  • C++ uses a class (with methods) std::string.

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-String are '\0' terminated.
  • C++ std::string are not null terminated and can contain any character
like image 25
Martin York Avatar answered Oct 25 '22 08:10

Martin York