Convert File Descriptor to ofstream object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe

    Convert File Descriptor to ofstream object

    Hi,
    I want to use the C library function mkstemp. It returns a unix file
    descriptor (int) to an open file. How do I convert the file descriptor to a
    valid ofstream object?
    Thanks,
    Joe


  • John Harrison

    #2
    Re: Convert File Descriptor to ofstream object


    "Joe" <joe.hesse@actc x.com> wrote in message
    news:401e7220$0 $41283$a1866201 @newsreader.vis i.com...[color=blue]
    > Hi,
    > I want to use the C library function mkstemp. It returns a unix file
    > descriptor (int) to an open file. How do I convert the file descriptor to[/color]
    a[color=blue]
    > valid ofstream object?
    > Thanks,
    > Joe[/color]

    There is no portable way to do that. Look to your compiler/library docs,
    some compilers (especially in Unix world) do support this.

    john


    Comment

    • John Harrison

      #3
      Re: Convert File Descriptor to ofstream object


      "John Harrison" <john_andronicu s@hotmail.com> wrote in message
      news:bvltd6$to0 p0$1@ID-196037.news.uni-berlin.de...[color=blue]
      >
      > "Joe" <joe.hesse@actc x.com> wrote in message
      > news:401e7220$0 $41283$a1866201 @newsreader.vis i.com...[color=green]
      > > Hi,
      > > I want to use the C library function mkstemp. It returns a unix file
      > > descriptor (int) to an open file. How do I convert the file descriptor[/color][/color]
      to[color=blue]
      > a[color=green]
      > > valid ofstream object?
      > > Thanks,
      > > Joe[/color]
      >
      > There is no portable way to do that. Look to your compiler/library docs,
      > some compilers (especially in Unix world) do support this.
      >
      > john[/color]

      The alternative, of course, is to write you own stream classes which can
      read/write to a file descriptor. Consult a good book on the STL to find out
      how to do that.

      john


      Comment

      • tom_usenet

        #4
        Re: Convert File Descriptor to ofstream object

        On Mon, 2 Feb 2004 09:51:58 -0600, "Joe" <joe.hesse@actc x.com> wrote:
        [color=blue]
        >Hi,
        >I want to use the C library function mkstemp. It returns a unix file
        >descriptor (int) to an open file. How do I convert the file descriptor to a
        >valid ofstream object?[/color]

        Since unix file descriptors are non-standard (not ISO-C++ standard,
        that is) it depends on your implementation. Most implementations
        provide an extension that allows you to do this. e.g.


        Tom

        C++ FAQ: http://www.parashift.com/c++-faq-lite/
        C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

        Comment

        • Gianni Mariani

          #5
          Re: Convert File Descriptor to ofstream object

          tom_usenet wrote:[color=blue]
          > On Mon, 2 Feb 2004 09:51:58 -0600, "Joe" <joe.hesse@actc x.com> wrote:
          >
          >[color=green]
          >>Hi,
          >>I want to use the C library function mkstemp. It returns a unix file
          >>descriptor (int) to an open file. How do I convert the file descriptor to a
          >>valid ofstream object?[/color]
          >
          >
          > Since unix file descriptors are non-standard (not ISO-C++ standard,
          > that is) it depends on your implementation. Most implementations
          > provide an extension that allows you to do this. e.g.
          > http://gcc.gnu.org/onlinedocs/libstd.../howto.html#11
          >
          > Tom
          >
          > C++ FAQ: http://www.parashift.com/c++-faq-lite/
          > C FAQ: http://www.eskimo.com/~scs/C-faq/top.html[/color]


          What Tom said - however, I do remember a recent posting citing a library
          called "fdstream". A google search seems to uncover some interesting links.


          Comment

          • Michael Mellor

            #6
            Re: Convert File Descriptor to ofstream object

            Gianni Mariani wrote:[color=blue]
            > tom_usenet wrote:
            >[color=green]
            >> On Mon, 2 Feb 2004 09:51:58 -0600, "Joe" <joe.hesse@actc x.com> wrote:
            >>
            >>[color=darkred]
            >>> Hi,
            >>> I want to use the C library function mkstemp. It returns a unix file
            >>> descriptor (int) to an open file. How do I convert the file
            >>> descriptor to a
            >>> valid ofstream object?[/color]
            >>
            >>
            >>
            >> Since unix file descriptors are non-standard (not ISO-C++ standard,
            >> that is) it depends on your implementation. Most implementations
            >> provide an extension that allows you to do this. e.g.
            >> http://gcc.gnu.org/onlinedocs/libstd.../howto.html#11
            >>
            >> Tom
            >>
            >> C++ FAQ: http://www.parashift.com/c++-faq-lite/
            >> C FAQ: http://www.eskimo.com/~scs/C-faq/top.html[/color]
            >
            > What Tom said - however, I do remember a recent posting citing a library
            > called "fdstream". A google search seems to uncover some interesting
            > links.
            >[/color]


            Michael Mellor

            Comment

            • Evan Carew

              #7
              Re: Convert File Descriptor to ofstream object

              Joe wrote:[color=blue]
              > Hi,
              > I want to use the C library function mkstemp. It returns a unix file
              > descriptor (int) to an open file. How do I convert the file descriptor to a
              > valid ofstream object?
              > Thanks,
              > Joe
              >
              >[/color]

              In the GCC tool chain, the following member header occurs in fstream:

              ofstream(int fd) : fstreambase(fd) { }

              this means that you could write something like:
              #include <fstream>
              #include <stdlib.h>
              #include <string.h>

              int main(){
              char *f_template;
              f_template = new char(12);
              memcpy(f_templa te, "/tmp/XXXXXX", 12);
              int fd = mkstemp(f_templ ate);
              cout << "File Descriptor # is: " << fd << " file name = " <<
              f_template <<endl;
              ofstream my_temp_out(fd) ;
              my_temp_out << "Some test text" << endl;
              while(1){} // Now take a look in your temp directory for the file
              //name printed from the above cout statement. If you cat it out,
              //you should see the "test text".
              return 0;
              }

              Comment

              • Jack Klein

                #8
                Re: Convert File Descriptor to ofstream object

                On Mon, 2 Feb 2004 09:51:58 -0600, "Joe" <joe.hesse@actc x.com> wrote
                in comp.lang.c++:
                [color=blue]
                > Hi,
                > I want to use the C library function mkstemp. It returns a unix file
                > descriptor (int) to an open file. How do I convert the file descriptor to a
                > valid ofstream object?
                > Thanks,
                > Joe[/color]

                There is no standard ANSI/ISO C library function mkstemp. It is a
                non-standard extension provided by your compiler/operating system.

                --
                Jack Klein
                Home: http://JK-Technology.Com
                FAQs for
                comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                alt.comp.lang.l earn.c-c++

                Comment

                • higi

                  #9
                  Re: Convert File Descriptor to ofstream object


                  On 4 Feb 2004 00:45:00 -0800 Vamat certificate 3, s_yuan31tee@yah oo.com
                  (sytee) wrote:
                  [color=blue]
                  >i have the following and it compile no problem
                  >
                  >example1:
                  >---------------------------------------------------------------------------[/color]
                  ----[color=blue]
                  >class Giant{
                  >public:
                  > Giant();
                  > int getHeight(){ return height; }
                  >
                  >private:
                  > static int height;
                  >};[/color]


                  This shouldn't really compile, because you have only declared 'height'.
                  In addition to being declared, it needs to be defined somewhere, in some
                  compilation unit. The definition looks like


                  int Giant::height = 0;


                  and in effect it reserves memory for the variable and defines an initial
                  value (which will be 0 if no initial value is specified).

                  The reason that it seems to compile OK without a definition might be that
                  you never actually use the Giant class.

                  When the class is used Microsoft Visual C++ 7.1 gives this error messaqe:


                  error LNK2019: unresolved external symbol "public: __thiscall
                  Giant::Giant(vo id)"
                  (??0Giant@@QAE@ XZ) referenced in function _main


                  [color=blue]
                  >---------------------------------------------------------------------------[/color]
                  ----[color=blue]
                  >
                  >i try to change the style of example1 to
                  >
                  >example2
                  >---------------------------------------------------------------------------[/color]
                  ----[color=blue]
                  >class Giant{
                  >public:
                  > Giant();
                  > int getHeight(); // here we change
                  >
                  >private:
                  > static int height;
                  >};
                  >
                  >int Giant::getHeigh t() {return height;}
                  >---------------------------------------------------------------------------[/color]
                  ----[color=blue]
                  >
                  >there will be error message saying that:
                  >test.obj : error LNK2001: unresolved external symbol "private: static
                  >int Giant::height" (?height@Giant@ @0HA)
                  >
                  >
                  >how can i solve the problem by using back the example2 style?[/color]
                  to Vamat
                  See above.

                  [color=blue]
                  >class Giant{
                  >public:
                  > Giant();
                  > int getHeight(){ return height; }
                  >
                  >private:
                  > static int height;
                  >};[/color]


                  only above is good ? :Vamat tester





                  Comment

                  Working...