473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Convert File Descriptor to ofstream object

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
Jul 22 '05 #1
8 9716

"Joe" <jo*******@actcx.com> wrote in message
news:40***********************@newsreader.visi.com ...
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
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bv************@ID-196037.news.uni-berlin.de...

"Joe" <jo*******@actcx.com> wrote in message
news:40***********************@newsreader.visi.com ...
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


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
Jul 22 '05 #3
On Mon, 2 Feb 2004 09:51:58 -0600, "Joe" <jo*******@actcx.com> wrote:
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/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
Jul 22 '05 #4
tom_usenet wrote:
On Mon, 2 Feb 2004 09:51:58 -0600, "Joe" <jo*******@actcx.com> wrote:

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/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

What Tom said - however, I do remember a recent posting citing a library
called "fdstream". A google search seems to uncover some interesting links.
Jul 22 '05 #5
Gianni Mariani wrote:
tom_usenet wrote:
On Mon, 2 Feb 2004 09:51:58 -0600, "Joe" <jo*******@actcx.com> wrote:

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/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


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

http://www.josuttis.com/cppcode/fdstream.html

Michael Mellor
Jul 22 '05 #6
Joe wrote:
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;
}

Jul 22 '05 #7
On Mon, 2 Feb 2004 09:51:58 -0600, "Joe" <jo*******@actcx.com> wrote
in comp.lang.c++:
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.

--
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
Jul 22 '05 #8

On 4 Feb 2004 00:45:00 -0800 Vamat certificate 3, s_*********@yahoo.com
(sytee) wrote:
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

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: wordsender | last post by:
Hey guys, I can't figure this one out, why is this simple script giving me problems? logfile=file(r'test.txt','w') logfile.write('datetime') test=logfile.readlines() When I run it I get...
4
by: Nicolas Fleury | last post by:
Hi, Is there any way to get the file descriptor on Unix or handle on Windows associated internally with a threading.Event object? So that it can be used in a call to select or...
6
by: Siddharth Taneja | last post by:
Hi, I have a very simple prg over here, trying to read the lines of a file #include <iostream> #include <fstream> #include <iostream> #include <string> using namespace std;
3
by: gipsy boy | last post by:
Given a file descriptor (from a network socket, for instance), I want to have an iostream that reads/writes to it, so I can push and read data in the traditional way : sockStream << "<some...
3
by: akaarj | last post by:
I am using Linux gcc 4.1.1 version I have a file descriptor and I want to have a FILE* pointer to the file from the file descriptor. OR if I can have the file name using the file descriptor.....
5
by: Joe Hesse | last post by:
Hi, I have a C++ function that writes to an ofstream object. I would like to sometimes use it to write to cout. I realize that cout is of type ostream which is not ofstream. Since cout is "kind...
5
by: yinglcs | last post by:
I have a c/c++ program in linux. I would like to know if I kill my program (e.g. exit(1)), will it release all the file descriptors my program held (regardless if I call close(fd) of each file...
9
by: Bill David | last post by:
I know it's very strange to do that since we have the file name when we call: int open(const char *pathname, int oflag,...); And we can store the file name for later usage. But I just wonder if...
1
by: zmohamed | last post by:
I'm passing an ofstream object to a function within my program. In that function, I'd like to extract the path and filename of the ofstream object, modify the filename only, create a new ofstream...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.