473,499 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get the file no from an ostream object?

Hi all,
Given something like:

std::ofstream out_file("path");

how do I extract the file descriptor from out_file? Is it possible?
What I want is to extract the file descriptor and then pass it to
flock like:

flock(???, LOCK_EX);

Any suggestion? Is it ok to mix flock with std::ofstream? If it's
dangerous, what options do I have? Thanks!
Jul 22 '05 #1
6 6253
On 26 Jul 2004 22:12:20 -0700, pembed2003 <pe********@yahoo.com> wrote:
Hi all,
Given something like:

std::ofstream out_file("path");

how do I extract the file descriptor from out_file? Is it possible?
What I want is to extract the file descriptor and then pass it to
flock like:

flock(???, LOCK_EX);

Any suggestion? Is it ok to mix flock with std::ofstream? If it's
dangerous, what options do I have? Thanks!


It's not possible with the standard file streams in standard C++.

Your only options are to write your own file stream classes, see The
C++ Standard Library by Josuttis for explanations and examples, or to find
someone else who has already done this work for you.

john
Jul 22 '05 #2
On 26 Jul 2004 22:12:20 -0700, pembed2003 wrote:
Hi all,
Given something like:

std::ofstream out_file("path");

how do I extract the file descriptor from out_file? Is it possible?
Nope. Seems an unfortunate omission. Anyone know why it was left out?
What I want is to extract the file descriptor and then pass it to
flock like:

flock(???, LOCK_EX);
What I did once when I needed a descriptor to pass to a legacy library was
to use fopen to open the file, and create an ofstream with the constructor
that takes a descriptor. Less than optimal, for sure, but it worked for my
purposes.
Any suggestion? Is it ok to mix flock with std::ofstream? If it's
dangerous, what options do I have? Thanks!


Don't know about flock, but I recall that there are buffering issues, so
you may need to flush whenever switching between the two modes of access.

--
Greg Schmidt gr***@trawna.com
Trawna Publications http://www.trawna.com/
Jul 22 '05 #3
On Tue, 27 Jul 2004 13:22:02 -0400, Greg Schmidt wrote:
On 26 Jul 2004 22:12:20 -0700, pembed2003 wrote:
Hi all,
Given something like:

std::ofstream out_file("path");

how do I extract the file descriptor from out_file? Is it possible?


Nope. Seems an unfortunate omission. Anyone know why it was left out?


Because there's no guarantee that there *is* an underlying file descriptor
in the sense that flock expects? Because if it *was* possible to extract
the file descriptor, it might not be in the state other code expects it to
be, and even if it was it very likely wouldn't stay that way?

Consider, if you will, that fstreams generally buffer their input and
output. A naive implementation of a "get_fd" function might not guarantee
that the buffers are flushed. And then, once you have it, how do you
propose to keep the file descriptor and the stream in something
approximating the same state? The whole thing's a mess, and best avoided.
What I want is to extract the file descriptor and then pass it to
flock like:

flock(???, LOCK_EX);


What I did once when I needed a descriptor to pass to a legacy library was
to use fopen to open the file, and create an ofstream with the constructor
that takes a descriptor. Less than optimal, for sure, but it worked for my
purposes.


I don't see a constructor that accepts an int in std::ostream.
Any suggestion? Is it ok to mix flock with std::ofstream? If it's
dangerous, what options do I have? Thanks!


Don't know about flock, but I recall that there are buffering issues, so
you may need to flush whenever switching between the two modes of access.


flock is defined as part of POSIX, not C or C++. Any interaction it has
with C or C++ is undefined as far as the C++ language is concerned.
However, you might be able to get away with opening the file a second time
with the POSIX open call (using the same filename) and using that handle
for flock. Just don't read or write from or to it.

Not guaranteed, of course. If you *really* need it and you want to do it
"right", use POSIX i/o for things that need flock.

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 22 '05 #4
On Wed, 28 Jul 2004 04:37:05 GMT, Owen Jacobson wrote:
On Tue, 27 Jul 2004 13:22:02 -0400, Greg Schmidt wrote:


[good answers to my questions deleted]
What I did once when I needed a descriptor to pass to a legacy library was
to use fopen to open the file, and create an ofstream with the constructor
that takes a descriptor. Less than optimal, for sure, but it worked for my
purposes.


I don't see a constructor that accepts an int in std::ostream.


Not std::ostream, std::ofstream. Maybe it's a VC++7.1 extension? My code
looks like this:

FILE *fd = fopen (filename.c_str(), "wb");
ofstream ofs (fd);

FILE appears to be a structure here, not just an int. I wouldn't think it
possible to silently convert a FILE* to a char* for the constructor, nor
would I think that the resulting ofs would be .good() if it did, but it all
works for me so it must be fully supported. I can't find now where it was
in the docs that told me I could do this (I didn't just try it on a whim),
but I've never been very impressed with the MS documentation so this is not
surprising.

--
Greg Schmidt gr***@trawna.com
Trawna Publications http://www.trawna.com/
Jul 22 '05 #5
On Wed, 28 Jul 2004 18:33:55 -0400, Greg Schmidt wrote:
On Wed, 28 Jul 2004 04:37:05 GMT, Owen Jacobson wrote:
On Tue, 27 Jul 2004 13:22:02 -0400, Greg Schmidt wrote:
[good answers to my questions deleted]
What I did once when I needed a descriptor to pass to a legacy library was
to use fopen to open the file, and create an ofstream with the constructor
that takes a descriptor. Less than optimal, for sure, but it worked for my
purposes.


I don't see a constructor that accepts an int in std::ostream.


Not std::ostream, std::ofstream. Maybe it's a VC++7.1 extension? My code
looks like this:


Which is what I actually meant to type. My apologies. I believe that
iostream.h, the pre-standardization version of the iostream header, and
fstream.h, likewise, did frequently have fstream classes that could accept
an existing FILE* or int during construction.
FILE *fd = fopen (filename.c_str(), "wb");
ofstream ofs (fd);

FILE appears to be a structure here, not just an int. I wouldn't think it
possible to silently convert a FILE* to a char* for the constructor, nor
would I think that the resulting ofs would be .good() if it did, but it all
works for me so it must be fully supported.
....on your specific compiler. Be wary of that; porting your code later
might be problematic.
I can't find now where it was in the docs that told me I could do this
(I didn't just try it on a whim), but I've never been very impressed
with the MS documentation so this is not surprising.


GNU libstdc++:
<http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-3.4/classstd_1_1basic__ofstream.html>

CPlusPlus.com reference docs:
<http://www.cplusplus.com/ref/iostream/ofstream/ofstream.html>

MSDN (VC++ 98):
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/html/FSTREAM_BASIC_OFSTREAM.asp>

If it was there, it's certainly gone now.

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 22 '05 #6
On Thu, 29 Jul 2004 03:31:09 GMT, Owen Jacobson wrote:
On Wed, 28 Jul 2004 18:33:55 -0400, Greg Schmidt wrote:
On Wed, 28 Jul 2004 04:37:05 GMT, Owen Jacobson wrote:
On Tue, 27 Jul 2004 13:22:02 -0400, Greg Schmidt wrote:
What I did once when I needed a descriptor to pass to a legacy library was
to use fopen to open the file, and create an ofstream with the constructor
that takes a descriptor. Less than optimal, for sure, but it worked for my
purposes.

I don't see a constructor that accepts an int in std::ostream.


Not std::ostream, std::ofstream. Maybe it's a VC++7.1 extension? My code
looks like this:


Which is what I actually meant to type. My apologies. I believe that
iostream.h, the pre-standardization version of the iostream header, and
fstream.h, likewise, did frequently have fstream classes that could accept
an existing FILE* or int during construction.


I'm definitely using fstream, not fstream.h.
FILE *fd = fopen (filename.c_str(), "wb");
ofstream ofs (fd);

FILE appears to be a structure here, not just an int. I wouldn't think it
possible to silently convert a FILE* to a char* for the constructor, nor
would I think that the resulting ofs would be .good() if it did, but it all
works for me so it must be fully supported.


...on your specific compiler.


That's what I meant, fully supported in my working environment. It also
probably helps me avoid synchronization and buffering issues that after
this initialization, I only access the file through one or the other
variable, not through both (there's a big switch, and I wanted to keep this
common code in one place rather than duplicating it for each case).
Be wary of that; porting your code later
might be problematic.


There's almost no chance of this code being ported, and if it is this will
be the least of my worries. I try to stick to standard, portable code
whenever possible, but this app is simply a GUI interface (to some custom
hardware) which is being used during development and testing, and will be
abandoned afterwards. The parts that will likely survive into the
follow-up app (e.g. the parts that deal with the hardware) are AFAIK all
standard compliant, but the GUI parts are all MFC, and the code in question
falls into the latter block.

Still, good to know that this isn't a general solution, and that it might
even go away with a compiler upgrade.

--
Greg Schmidt gr***@trawna.com
Trawna Publications http://www.trawna.com/
Jul 22 '05 #7

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

Similar topics

0
2054
by: Ryan M. Keith | last post by:
I am having a problem with the ostream operator in templated classes that I wrote (I'm using the Borland compiler), and I'm certain that the templates are the problem because when I remove the...
2
5949
by: keit6736 | last post by:
Hi, I'm using the Borland compiler and I've created two templated classes in which I've overloaded the ostream << operator. However, when I try and use the operator on objects of either class I...
1
3241
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
6
4594
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
2
8302
by: plank | last post by:
Hey Peeps, Ok here is my situation.. I have a Java applet which allows the user to select files and upload them to the server. The applet converts the file to Base64 and then POSTS the data to an...
11
4007
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
0
1831
by: AirYT | last post by:
Hello, Here's a quick explanation & problem: i have an ASP (iis v5.0) application that generates a pdf file and this file is saved to the server. it is saved in a location not available to web...
2
4211
by: Seok Bee | last post by:
Dear Experts, In my web application, I am having a button to open a file located in the server. When I click on the button to view the file, I received the following error message:...
5
12024
by: twiggy182 | last post by:
Hi, I really need you help because I'm not very familliar with ASP and I could not find any solution to my problem. To put you in situation, I have a CGI to which I send a file name, and that...
5
3924
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, The following code was suggested by one of the users in this newsgroup when a pdf file was requested by a user from an asp page. I used the similar code in my page and the very interesting...
0
7132
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
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7390
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...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.