Standard stream from C fopen()

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Standard stream from C fopen()

Claude
Hi!

I Use this code for make a standard stream from a C fopen() FILE* :

    FILE *fd = fopen("Prova", "w");
        if (fd)
        {
         
          //  io::stream_buffer<io::file_descriptor_source> fpstream (fileno(fd));
          //std::istream in (&fpstream);

        istream in = boost::iostreams::file_descriptor(fileno(fd));
      }

But I obtain an error (G++ on Linux Ubuntu):

[...]
error: conversion from ‘boost::iostreams::file_descriptor’ to non-scalar type ‘std::istream’ requested|
[...]
error: call of overloaded ‘path(const int&)’ is ambiguous|
[...]


Why?
What is the correct method?




Reply | Threaded
Open this post in threaded view
|

Re: Standard stream from C fopen()

Eric MALENFANT
De :Claude

>
> I Use this code for make a standard stream from a C fopen() FILE* :
>
>     FILE *fd = fopen("Prova", "w");
>         if (fd)
>         {
>
>           //  io::stream_buffer<io::file_descriptor_source> fpstream
> (fileno(fd));
>           //std::istream in (&fpstream);
>
>         istream in = boost::iostreams::file_descriptor(fileno(fd));
>       }
>
> But I obtain an error (G++ on Linux Ubuntu):

file_descriptor is a "Device", not a "stream". To make a stream out of a Device, wrap the latter into a stream. From the description of stream in the docs: http://www.boost.org/doc/libs/1_44_0/libs/iostreams/doc/index.html?page=http://www.boost.org/doc/libs/1_44_0/libs/iostreams/doc/guide/generic_streams.html%23stream

"
Stream template which performs i/o by delegating to a contained Device. The Device type is specified as the first template parameter to stream. Instances of the the Device type are attached and detached using the member functions open and close.

The template stream derives from a specialization of std::basic_istream, std::basic_ostream or std::basic_iostream, depending on whether the underlying Device models Source, Sink or both..
"


Note: I believe this question would be more suited for the "users" mailing list


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: Standard stream from C fopen()

Claude
So these lines are right?

  stream_buffer<file_descriptor_source> fpstream  (fileno(fd));
  std::istream in (&fpstream);

I making a stream_buffer from with a file_descriptor_source as template parameter...

Excuse me , but I don't know well the C++...

OT
How can I move this question in "user" mailing list?
/OT


Reply | Threaded
Open this post in threaded view
|

Re: Standard stream from C fopen()

Eric MALENFANT
De : Claude
>
> So these lines are right?
>
>   stream_buffer<file_descriptor_source> fpstream  (fileno(fd));
>   std::istream in (&fpstream);
>
> I making a stream_buffer from with a file_descriptor_source as template
> parameter...

Almost. You should create the file_descriptor_source to pass it to the stream_buffer's constructor:

  stream_buffer<file_descriptor_source> fpstream(file_descriptor_source(fileno(fp)));
  std::istream in (&fpstream);

You could also avoid the intermediate stream_buffer by simply writing:

  stream<file_descriptor_source> in(file_descriptor_source(fileno(fp)));

instead, because a boost::iostreams::stream<something_that_is_a_source> inherits from std::istream

(BTW, don't forget to fclose()!)

> Excuse me , but I don't know well the C++...

No problem, it happened even to the best here :)

 
> OT
> How can I move this question in "user" mailing list?
> /OT

See http://www.boost.org/community/groups.html#users

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: Standard stream from C fopen()

Claude
Uhmm

I obtain always this error:

error: call of overloaded ‘path(const int&)’ is ambiguous|

Reply | Threaded
Open this post in threaded view
|

Re: Standard stream from C fopen()

Claude
Ok, I now make correctly my stream with:

stream<file_descriptor_source> in(file_descriptor_source(fp));

But, if I use the << operator on "in" with:

 
  in<<"Hello";

It don't work :-(
I think I could use my object like any other stream ...


Reply | Threaded
Open this post in threaded view
|

Re: Standard stream from C fopen()

Jeff Flinn
Claude wrote:

> Ok, I now make correctly my stream with:
>
> stream<file_descriptor_source> in(file_descriptor_source(fp));
>
> But, if I use the << operator on "in" with:
>
>  
>   in<<"Hello";
>
> It don't work :-(
> I think I could use my object like any other stream ...

A source can't be written to. Did you mean:

   in >> some_var;

or if you want to write to the stream you would use:

   stream<file_descriptor_sink> out(file_descriptor_sink(fp));

   out << "Hello";

I don't recall if theres a file_descriptor that allows io.

Jeff

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: Standard stream from C fopen()

Daniel James-3
In reply to this post by Claude
On 13 November 2010 06:11, Claude <[hidden email]> wrote:
>
> Uhmm
>
> I obtain always this error:
>
> error: call of overloaded ‘path(const int&)’ is ambiguous|

The library changed in 1.44, you now need to explicitly specify if you
want the file_descriptor to be automatically closed.

    namespace io = boost::iostreams;

    io::stream<io::file_descriptor_source> in(
        handle, io::close_handle);

And the handle will be closed when in is destructed, or you call
'in.close()'. Or if you want to close it yourself:

    io::stream<io::file_descriptor_source> in(
        handle, io::never_close_handle);

    // later...

    close(handle);

Note that with 'never_close_handle', the handle isn't closed even if
you call the 'in.close()'. This is because otherwise some generic code
can call 'in.close' and close the file when it shouldn't be closed.

Daniel
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: Standard stream from C fopen()

Daniel James-3
In reply to this post by Claude
On 13 November 2010 23:49, Claude <[hidden email]> wrote:
>
> Ok, I now make correctly my stream with:
>
> stream<file_descriptor_source> in(file_descriptor_source(fp));

Sadly, that doesn't mean what you think it means. You've declared a
function which takes a file_desciptor_source as a parameter. This is
known as "C++'s most vexing parse", after an article about it in the
book Effective C++. If you don't have that book, there's an article
about it here:

http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=439

The errors that you're getting should have the type of 'in' as a
function, which is the clue. Also, if you use 'clang' it gives a
helpful warning.

Daniel
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost