opening a file Descriptor

This is a discussion on opening a file Descriptor within the C++ Programming forums, part of the General Programming Boards category; Hello all, iam working on some project in which i need to open a file descriptor and send it to ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    opening a file Descriptor

    Hello all,
    iam working on some project in which i need to open a file descriptor and send it to the streambuf,
    which iam doing like
    Code:
    ifstream f(fd);
    streambuf * buf2=f.rdbuf();
    but main problem is that they are casting to
    Code:
    ifstream f((const char *)fd)
    streambuf * buf2=f.rdbuf();
    because of which the file is not getting opened ,if i remove that leads to error there are using some conventions

    there are many ways are also there like
    Code:
    File *ptr=fdopen(fd);
    which is successfull and returning the value.
    but i want the the output to be given in streambuf which other parts of my project is using,
    can u tell me is there any way to convert the file pointer to the streambuf or is there anyway of reading the file so that the output will result in the streambuf
    with this convention
    Code:
    ((const char *)fd)
    please help me.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    An ifstream takes a filename as the constructor parameter. That's because a filename is the only remotely portable thing.

    An ifstream does not take a file descriptor as the constructor parameter. Nor will you find a way to convince it to.

    You pretty much have to write your own stream buffer for file descriptors. Or you might find a pre-written one. Perhaps your standard library even supports it as an extension. Or it might support creating a file stream from a FILE*; then you could use fdopen().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    thank u for quick reply,

    please can u tell is there any way i can convert filepointer to
    streambuf, or as u told

    might find a pre-written one

    if u have any examples please help me, iam totally trying all the possibility
    but nothing is working please help me.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You'd have to wrap calls to fopen(), fdopen(), or other similar items. That is what I think CornedBee is telling you.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Hello all,
    please still if u have any example or code or still more explanation will be
    very helpful because almost all i tried from all perspective,
    asking for expert suggestion or help .

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    My guess would be you would create your own class derived from streambuf, and then hide the fd inside that class.

    Then provide appropriate member functions which work on a streambuf which do the right thing with the fd inside the class.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21