C++流问题(代码中注释的解释)

发布于 2024-10-16 20:22:15 字数 586 浏览 2 评论 0原文

我正在使用 此处 找到的 fastCGI 应用程序。

代码中的注释如下:

    if (content) delete []content;

    // If the output streambufs had non-zero bufsizes and
    // were constructed outside of the accept loop (i.e.
    // their destructor won't be called here), they would
    // have to be flushed here.

我对 C++ 流的了解相当薄弱。有人可以解释一下以下内容:

  1. 评论中引用了哪些streambufs?
  2. 在什么条件下,streambufs 的 bufsize 会非零?

最后但并非最不重要的一点是,有人可以指出一个在线资源(双关语),它提供了对 C++ IO 流的清晰但温和的介绍吗?

I am playing around with the fastCGI application found here.

The following comment is in the code:

    if (content) delete []content;

    // If the output streambufs had non-zero bufsizes and
    // were constructed outside of the accept loop (i.e.
    // their destructor won't be called here), they would
    // have to be flushed here.

My knowledge of C++ streams is rather weak. Could someone please explain the following:

  1. which streambufs are being referred to in the comment?
  2. under what conditions would the streambufs had non-zero bufsizes?

last but not the least, can someone point to a resource (pun intended) online that provides a clear but gentle introduction to C++ IO streams?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2024-10-23 20:22:15

评论中引用了哪些streambuf?

它指的是 request.out,它是重新分配的 cout 的一部分:

FCGX_Request request;
...
    fcgi_streambuf cout_fcgi_streambuf(request.out);
    ...
    cout = &cout_fcgi_streambuf;

此重新分配意味着用户可以调用

cout << "Content-type: text/html\r\n"
     << ...

并在任一控制台上显示文本(例如测试)或作为 CGI 应用程序运行时通过网络。因此,一个代码示例可以在多种环境中运行。


在什么条件下,streambufs 的 bufsize 会非零?

您提到的评论是一个友好的提醒。此示例代码中的 fcgi_streambuf 对象是在循环中构造的;当循环结束时,它们超出范围并因此被破坏。

该评论警告说,在许多情况下,用户必须刷新输出流:

cout.flush ();

which streambufs are being referred to in the comment?

It's referring to request.out, which is part of the reassigned cout:

FCGX_Request request;
...
    fcgi_streambuf cout_fcgi_streambuf(request.out);
    ...
    cout = &cout_fcgi_streambuf;

This reassignment means that the user can call

cout << "Content-type: text/html\r\n"
     << ...

and have the text show-up on either the console (for testing) or across the network when run as a CGI application. Thus, one code sample can run in multiple environments.


under what conditions would the streambufs had non-zero bufsizes?

The comment you're referring to is a friendly reminder. The fcgi_streambuf objects in this sample code are constructed in a loop; when the loop ends, they go out of scope and are thus destructed.

The comment warns that in many circumstances, the user would have to flush the output stream:

cout.flush ();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文