file descriptor and fstream object - Unix

This is a discussion on file descriptor and fstream object - Unix ; How to find the fd of a ifstream/ofstream object or create an ifstream/ofstream object using standard POSIX functions?...

+ Reply to Thread
Results 1 to 4 of 4

Thread: file descriptor and fstream object

  1. file descriptor and fstream object

    How to find the fd of a ifstream/ofstream object or create an
    ifstream/ofstream object using standard POSIX functions?

  2. Re: file descriptor and fstream object

    On Nov 9, 1:14 pm, Michael wrote:
    > How to find the fd of a ifstream/ofstream object or create an
    > ifstream/ofstream object using standard POSIX functions?


    For the first question, you can't. The only reason you can use POSIX
    from C++ is because C++ is 'backwards-compatible'[*] with C.
    As for the second one, why would you want a POSIX function to create
    an fstream object? Just use the functions available to you by standard
    C++. (ie fstream:pen)

    It'd be better if you described your real problem (rather than what
    you think would solve the problem).
    [*] Not really. See

  3. Re: file descriptor and fstream object

    vippstar@gmail.com wrote:
    > On Nov 9, 1:14 pm, Michael wrote:
    >> How to find the fd of a ifstream/ofstream object or create an
    >> ifstream/ofstream object using standard POSIX functions?

    >
    > For the first question, you can't. The only reason you can use POSIX
    > from C++ is because C++ is 'backwards-compatible'[*] with C.
    > As for the second one, why would you want a POSIX function to create
    > an fstream object? Just use the functions available to you by standard
    > C++. (ie fstream:pen)
    >
    > It'd be better if you described your real problem (rather than what
    > you think would solve the problem).
    >
    >[*] Not really. See

    I think that using "magic numbers" for processing files is unsafe. I
    want to write some functions for class ifstream and ofstream to wrap
    these unsafe operations and hide the "magic numbers" from users.

  4. Re: file descriptor and fstream object

    On Nov 10, 4:50*am, Michael wrote:
    > vipps...@gmail.com wrote:
    > > On Nov 9, 1:14 pm, Michael wrote:
    > >> How to find the fd of a ifstream/ofstream object or create an
    > >> ifstream/ofstream object using standard POSIX functions?

    >
    > > For the first question, you can't. The only reason you can use POSIX
    > > from C++ is because C++ is 'backwards-compatible'[*] with C.
    > > As for the second one, why would you want a POSIX function to create
    > > an fstream object? Just use the functions available to you by standard
    > > C++. (ie fstream:pen)

    >
    > > It'd be better if you described your real problem (rather than what
    > > you think would solve the problem).

    >
    > >[*] Not really. See

    >
    > I think that using "magic numbers" for processing files is unsafe.


    Which magic numbers? STDIN_FILENO and friends or file descriptors 0,
    1, 2?

    In either case POSIX guarantees what file descriptors 0, 1 and 2 refer
    to: http://www.opengroup.org/onlinepubs/...ons/stdin.html

    > I want to write some functions for class ifstream and ofstream to wrap
    > these unsafe operations and hide the "magic numbers" from users.


    You can write your own std::basic_steambuf<> derived class that wraps
    file descriptors. It is a bit tricky provided often unnecessary
    complexity and subtleties of basic_steambuf, so you might like to use
    a helper library for that http://www.boost.org/doc/libs/1_37_0...doc/index.html

    --
    Max

+ Reply to Thread