Connecting Tech Pros Worldwide Help | Site Map

Convert File Descriptor to ofstream object

Joe
Guest
 
Posts: n/a
#1: Jul 22 '05
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
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Convert File Descriptor to ofstream object



"Joe" <joe.hesse@actcx.com> wrote in message
news:401e7220$0$41283$a1866201@newsreader.visi.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


John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Convert File Descriptor to ofstream object



"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:bvltd6$to0p0$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "Joe" <joe.hesse@actcx.com> wrote in message
> news:401e7220$0$41283$a1866201@newsreader.visi.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


tom_usenet
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Convert File Descriptor to ofstream object


On Mon, 2 Feb 2004 09:51:58 -0600, "Joe" <joe.hesse@actcx.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.
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
Gianni Mariani
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Convert File Descriptor to ofstream object


tom_usenet wrote:[color=blue]
> On Mon, 2 Feb 2004 09:51:58 -0600, "Joe" <joe.hesse@actcx.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.


Michael Mellor
Guest
 
Posts: n/a
#6: Jul 22 '05

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@actcx.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]
http://www.josuttis.com/cppcode/fdstream.html

Michael Mellor
Evan Carew
Guest
 
Posts: n/a
#7: Jul 22 '05

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_template, "/tmp/XXXXXX", 12);
int fd = mkstemp(f_template);
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;
}

Jack Klein
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Convert File Descriptor to ofstream object


On Mon, 2 Feb 2004 09:51:58 -0600, "Joe" <joe.hesse@actcx.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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
higi
Guest
 
Posts: n/a
#9: Jul 22 '05

re: Convert File Descriptor to ofstream object



On 4 Feb 2004 00:45:00 -0800 Vamat certificate 3, s_yuan31tee@yahoo.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(void)"
(??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::getHeight() {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





Closed Thread


Similar C / C++ bytes