Note that there are some explanatory texts on larger screens.

plurals
  1. POConnecting std::basic_ofstream<unsigned char> to a FIFO. bad_cast exceptions
    primarykey
    data
    text
    <p>Using gcc 4.4.3 on Linux 2.6.32, I get bad_cast exceptions when connecting std::basic_ofstream to a FIFO.</p> <p>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.</p> <p>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? </p> <p>Thanks in advance.</p> <p>EDIT: Adding source code.</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;stdio.h&gt; #include &lt;sys/stat.h&gt; #include &lt;pthread.h&gt; using namespace std; //#define CHAR char #define CHAR unsigned char bool still_writing = true; void* reader (void*) { basic_ifstream&lt;CHAR&gt; in ("FIFO", fstream::in); basic_streambuf&lt;CHAR&gt; *stream_buffer = in.rdbuf(); bool no_reads = true; CHAR buffer[10]; while (stream_buffer-&gt;in_avail() || still_writing || no_reads) { in.readsome(buffer, 10); const int got = (int)in.gcount(); if (got &gt; 0) { std::cerr &lt;&lt; "Got something!\n"; no_reads = false; } } } int main () { mkfifo ("FIFO", S_IRUSR | S_IWUSR); pthread_t reader_thread_id; pthread_create (&amp;reader_thread_id, NULL, reader, NULL); basic_ofstream&lt;CHAR&gt; out ("FIFO"); out &lt;&lt; (CHAR) 70; still_writing = false; out.flush(); pthread_join (reader_thread_id, NULL); remove ("FIFO"); } </code></pre> <p>In this version, the exception arises from <code>stream_buffer-&gt;in_avail()</code> If you swap the <code>#define</code> statements, everything is fine.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload