Convert File Descriptor to ofstream object

J

Joe

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
 
J

John Harrison

Joe said:
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

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

john
 
J

John Harrison

John Harrison said:
to

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

john

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
 
T

tom_usenet

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?

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/libstdc++/27_io/howto.html#11

Tom

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

Gianni Mariani

tom_usenet said:
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/libstdc++/27_io/howto.html#11

Tom

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


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

Evan Carew

Joe said:
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

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;
}
 
J

Jack Klein

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

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

higi

i have the following and it compile no problem

example1:
--------------------------------------------------------------------------- ----
class Giant{
public:
Giant();
int getHeight(){ return height; }

private:
static int height;
};


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


--------------------------------------------------------------------------- ----

i try to change the style of example1 to

example2
--------------------------------------------------------------------------- ----
class Giant{
public:
Giant();
int getHeight(); // here we change

private:
static int height;
};

int Giant::getHeight() {return height;}
--------------------------------------------------------------------------- ----

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?
to Vamat
See above.

class Giant{
public:
Giant();
int getHeight(){ return height; }

private:
static int height;
};


only above is good ? :Vamat tester
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top