سؤال

هل يمكن لأحد أن يكتب بعض رمز العينة لشرح هذا المفهوم؟ أعرف ما الذي يستخدمه الدفق المخزّن ، لكني أود أيضًا معرفة كيفية تنفيذ ذلك.

شكرا مقدما!

هل كانت مفيدة؟

المحلول

يمكنك النظر في تطبيق النظام الأساسي الخاص بك ، معيار C ++ أو "قياسي C ++ IoStreams والألغام" من تأليف أنجيليكا لانجر وكلاوس كريت.

كن مستعدًا لمنحنى تعليمي. الجداول قديمة ومسألة معقدة. (فرانسيس جلاسبورو: "لدي القليل جدًا من الشكوك بأن مكتبات I/O هي من بين أصعب الجوانب في أي لغة.")

نصائح أخرى

Veeeery بشكل تخطيطي ، لتيار "إدخال":

class BufferedInputStream
{
public:
  BufferedInputStream(SomeExternalDevice d)
  : m_Device(d),
    m_Current(m_Buffer),
    m_End(m_Buffer)
  {}

  char get(){
    if (!IsDataAvailableInBuffer()){
      ReadAChunkFromDiskAndPutItInBuffer();
    }
    return PopByteFromBuffer();
  }

private:

  bool IsDataAvailableInBuffer()const{
    return m_Current != m_End;
  }

  void ReadAChunkFromDiskAndPutItInBuffer(){
    // Buffer must be empty
    assert(!IsDataAvailableInBuffer());

    // Read bytes from the device
    bytes_read = Read(m_Device, m_Buffer, BufferSize);

    // Reposition the "begin" and "end" pointers
    m_Current = m_Buffer;
    m_End = m_Buffer + bytes_read;
  }

  char PopByteFromBuffer(){
    assert(IsDataAvailableInBuffer());
    return *m_Current++;
  }

  // For example, an OS file handle
  SomeExternalDevice m_Device;

  // The buffer itself
  char m_Buffer[BufferSize];

  // Begin of buffer contents
  char* m_Current;

  // End of buffer contents
  char* m_End;
};

وبهذه الطريقة ، تتم قراءة البيانات من القرص في أجزاء من حجم المخزن المؤقت ، ومعظم المكالمات إلى "الحصول على ()" لا يجب أن ينتهي بها المطاف في المكالمات إلى نظام التشغيل ، حيث يمكنهم ببساطة إعادة بايت من المخزن المؤقت.

ألق نظرة على تطبيق STL لSstreamوsstream.tcc (روابط لتنفيذ SGI STL).

القاعدة stringstream الفصل هو basic_stringstream, الذي ينفذ basic_iostream واجهه المستخدم.

  // [27.7.4] Template class basic_stringstream
  /**
   *  @brief  Controlling input and output for std::string.
   *
   *  This class supports reading from and writing to objects of type
   *  std::basic_string, using the inherited functions from
   *  std::basic_iostream.  To control the associated sequence, an instance
   *  of std::basic_stringbuf is used, which this page refers to as @c sb.
  */

هناك فئة قاعدة basic_stringbuf التي تستمد من basic_streambuf. هذا يحمل المخزن المؤقت.

  // [27.7.1] template class basic_stringbuf
  /**
   *  @brief  The actual work of input and output (for std::string).
   *
   *  This class associates either or both of its input and output sequences
   *  with a sequence of characters, which can be initialized from, or made
   *  available as, a @c std::basic_string.  (Paraphrased from [27.7.1]/1.)
   *
   *  For this class, open modes (of type @c ios_base::openmode) have
   *  @c in set if the input sequence can be read, and @c out set if the
   *  output sequence can be written.
  */
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top