1

I have old code running in Solaris that I am porting to linux. I already changed the header file from <fstream.h> to the standard fstream. the class is:

    class UNITSocketStream : public fstream
    {
    public:
      UNITSocketStream( UNITSocket & );
      UNISocketStream( int sfd );
      virtual ~UNITSocketStream();
    private:
      bool owned;
      UNITSocketStream(const UNITSocketStream &);
      UNITSocketStream & operator =( const UNITSocketStream &rhs);
};

and in the file cpp I have the initialization list:

UNITSocketStream::UNITSocketStream( UNITSocket &sockobj )
: fstream( sockobj.sockfd())
{
  if ( fail() )
  {
    throw UNITSocketStreamInitFail(" bad stream ");
  }
  sockobj.disown();
}

// ============================================================================
UNITSocketStream::UNITSocketStream(int sockfd)
: fstream( sockfd ) 
{
  if ( fail() )
  {
    throw UNITSocketStreamInitFail(" bad stream ");
  }
}

So now When I compile I receive the error:

error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive] : fstream( sockobj.sockfd())

error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive] : fstream( sockfd ) in the lines where is invoked fstream().

Since in Solaris is available (LINK):

fstream(int fileno);

and in linux no, how I could traslate this call of fstream()?

Apollo17
  • 35
  • 7
  • There's no `fstream` constructor, which takes a raw filedescriptor. – πάντα ῥεῖ Oct 14 '21 at 11:51
  • so you mean that when I open a socket, creating a file descriptor, I can't manage it with a derived class of fstream? Do I need only a string to manage a basic_fstream? – Apollo17 Oct 14 '21 at 12:12
  • 1
    The key point is to implement an appropriate [`std::streambuf`](https://en.cppreference.com/w/cpp/io/basic_streambuf), the implementation can interact with the filedescriptor internally. – πάντα ῥεῖ Oct 14 '21 at 12:30
  • @πάνταῥεῖ A pure hack that *might* work on Linux is something like `snprintf(buffer,"/proc/self/fd/%d",sockfd)` and then create the stream with `fstream(buffer)`. It's definitely playing with fire, though :-) – Andrew Henle Oct 15 '21 at 17:34

0 Answers0