简体   繁体   English

我可以在C#中使用stringstream作为内存流,如`MemoryStream`吗?

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

In C/C++, strings are NULL terminated. 在C / C ++中,字符串以NULL结尾。

Could I use stringstream as a memory stream like MemoryStream in C#? 我可以在C#中使用stringstream作为MemoryStream等内存流吗?

Data of memory streams may have \\0 values in the middle of data, but C++ strings are NULL terminated. 内存流的数据在数据中间可能具有\\0值,但C ++字符串以NULL结尾。

When storing character sequences in a std::string you can have included null characters. std::string存储字符序列时,可以包含空字符。 Correspondingly, a std::stringstream can deal with embedded null characters as well. 相应地, std::stringstream也可以处理嵌入的空字符。 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, ie, you'd need to use the various overloads taking the size of the character sequence as argument. 此外,当使用内置字符串为std::string赋值时,空字符将很重要,即,您需要使用以字符序列的大小作为参数的各种重载。

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. 在C / C ++中,字符串以NULL结尾。

  • 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). 但是C与C ++差不多,就像Jave对C#一样(因为它们是不同的)。

Each language has their own string features. 每种语言都有自己的字符串功能。

  • C uses a sequence of bytes that by convention is terminated by a '\\0' byte. C使用按照惯例以'\\ 0'字节终止的字节序列。
    • Commonly referred to as a C-String. 通常称为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程序中一个巨大的bug区域。
  • C++ uses a class (with methods) std::string. C ++使用一个类(带有方法)std :: string。

Could I use stringstream as a memory stream like MemoryStream in C#? 我可以在C#中使用stringstream作为MemoryStream等内存流吗?

Sure. 当然。 The Memory stream is a stream that is backed by memory (rather than a file). 内存流是由内存(而不是文件)支持的流。 This is exactly what std::stringstream is. 这正是std :: stringstream的意思。 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. 内存流的数据在数据中间可能具有\\ 0值,但C ++字符串以NULL结尾。

This is totally incorrect. 这完全是错误的。

  • C-String are '\\0' terminated. C-String被'\\ 0'终止。
  • C++ std::string are not null terminated and can contain any character C ++ std :: string不是以null结尾,可以包含任何字符

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() . C和C ++是两种不同的语言,C序列中的连续字符表示字符串,您可以使用memcpy()memset()memcmp()将其视为可以设置/获取其值的内存部分。

In C++ strings means a class of information which used to get correct data as string. 在C ++中,字符串表示用于将正确数据作为字符串获取的一类信息。 So you can't treat it as a sequence of memory location with char type. 因此,您不能将其视为具有char类型的内存位置序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM