连接 std::basic_ofstream到 FIFO。 bad_cast 异常

发布于 2024-09-04 23:14:39 字数 1492 浏览 5 评论 0原文

在 Linux 2.6.32 上使用 gcc 4.4.3,将 std::basic_ofstream 连接到 FIFO 时出现 bad_cast 异常。

单步调试器,我可以看到错误是在标准库中的各个位置生成的,因为流或 filebuf 对象的 _M_codecvt 成员为 NULL。它发生的确切位置取决于操作的顺序,但每个操作的原因似乎都是相同的。

那么我在这里做了一些根本上愚蠢的事情吗? ofstream 和 ifstream 工作正常。是否有某种原因表明您不应该将除 char 之外的任何内容附加到 FIFO?

提前致谢。

编辑:添加源代码。

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sys/stat.h>
#include <pthread.h>
using namespace std;

//#define CHAR char
#define CHAR unsigned char
bool still_writing = true;

void* reader (void*)
{
    basic_ifstream<CHAR> in ("FIFO", fstream::in);
    basic_streambuf<CHAR> *stream_buffer = in.rdbuf();

    bool no_reads = true;
    CHAR buffer[10];

    while (stream_buffer->in_avail() ||
           still_writing || 
           no_reads)
    {
        in.readsome(buffer, 10);
        const int got = (int)in.gcount();
        if (got > 0) {
            std::cerr << "Got something!\n";
            no_reads = false;
        }
    }
}

int main ()
{
    mkfifo ("FIFO", S_IRUSR | S_IWUSR);

    pthread_t reader_thread_id;
    pthread_create (&reader_thread_id, NULL, reader, NULL);

    basic_ofstream<CHAR> out ("FIFO");
    out << (CHAR) 70;
    still_writing = false;
    out.flush();

    pthread_join (reader_thread_id, NULL);

    remove ("FIFO");
}

在此版本中,异常源自 stream_buffer->in_avail() 如果交换 #define 语句,则一切正常。

Using gcc 4.4.3 on Linux 2.6.32, I get bad_cast exceptions when connecting std::basic_ofstream to a FIFO.

Stepping though the debugger, I can see that the error is generated at various places in the standard library because the _M_codecvt member of the stream or filebuf object is NULL. Exactly where it happens depends on the order of operations, but it appears to be the same cause in each.

So am I doing something fundamentally stupid here? ofstream and ifstream work fine. Is there some reason that you shouldn't attach a stream of anything besides char to a FIFO?

Thanks in advance.

EDIT: Adding source code.

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sys/stat.h>
#include <pthread.h>
using namespace std;

//#define CHAR char
#define CHAR unsigned char
bool still_writing = true;

void* reader (void*)
{
    basic_ifstream<CHAR> in ("FIFO", fstream::in);
    basic_streambuf<CHAR> *stream_buffer = in.rdbuf();

    bool no_reads = true;
    CHAR buffer[10];

    while (stream_buffer->in_avail() ||
           still_writing || 
           no_reads)
    {
        in.readsome(buffer, 10);
        const int got = (int)in.gcount();
        if (got > 0) {
            std::cerr << "Got something!\n";
            no_reads = false;
        }
    }
}

int main ()
{
    mkfifo ("FIFO", S_IRUSR | S_IWUSR);

    pthread_t reader_thread_id;
    pthread_create (&reader_thread_id, NULL, reader, NULL);

    basic_ofstream<CHAR> out ("FIFO");
    out << (CHAR) 70;
    still_writing = false;
    out.flush();

    pthread_join (reader_thread_id, NULL);

    remove ("FIFO");
}

In this version, the exception arises from stream_buffer->in_avail() If you swap the #define statements, everything is fine.

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

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

发布评论

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

评论(1

來不及說愛妳 2024-09-11 23:14:39

我有类似的问题。我发现不能设置 skipws 标志。确保使用 std::noskipws(your_stream_obj)。我将其调试到 gcc 代码中并找出了发生这种情况的原因,但已经忘记了。

希望有帮助...

I had a similar issue. What I found out is that setting a skipws-flag must not be set. To ensure that use std::noskipws(your_stream_obj). I debugged it into the gcc-code and found out why that happened, but forgot it already.

Hope that helps...

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