Operating System - HP-UX
1753792 Members
7153 Online
108799 Solutions
New Discussion юеВ

Re: class "std::basic_ofstream attach function

 
SOLVED
Go to solution
sushant keerti
Advisor

class "std::basic_ofstream attach function

Hi,
We are migrating our application from hp-ux 11.0 PA to 11.23 IPF. We are getting the below error while compiling.
class "std::basic_ofstream>" has no member "attach"
logDest.attach(2);
^

( note : logDest has been declared as
ofstream logDest; )
I found that the standard C++ runtime doesnot have anymore the attach member function for the class std::basic_ofstream.

Could anybody let me know the altarnative functionality, which works same as "attach"?.

11 REPLIES 11
Dennis Handly
Acclaimed Contributor
Solution

Re: class "std::basic_ofstream attach function

cfront's fstream::attach & detach aren't Standard and don't exist in -AA mode.

There is a non-Standard replacement for attach, so it isn't mentioned in the porting page:
http://h21007.www2.hp.com/portal/site/dspp/menuitem.863c3e4cbcdc3f3515b49c108973a801/?ciid=eb08b3f1eee02110b3f1eee02110275d6e10RCRD

It does mention detach is missing.

To do ofstream::attach you can do following overloaded extension to open:
void open(int __fd, char_type *__buf=0,
streamsize __n=_RWSTD_DEFAULT_BUFSIZE)
sushant keerti
Advisor

Re: class "std::basic_ofstream attach function

Dennis,
Just a confirmation...To attach the STDOUT to logDest, I am writing code as below. Is it rite?

ofstream logDest;
logDest.open(1, 0, _RWSTD_DEFAULT_BUFSIZE);
Dennis Handly
Acclaimed Contributor

Re: class "std::basic_ofstream attach function

>To attach the STDOUT to logDest, I am writing code as below.

Well, I would take the defaults and just use:
#include
logDest.open(STDOUT_FILENO);

If you aren't using "<< endl" everywhere, you might want your buffer even bigger.
Dennis Handly
Acclaimed Contributor

Re: class "std::basic_ofstream attach function

If you have gotten the answers you needed, please read the following about assigning points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33
sushant keerti
Advisor

Re: class "std::basic_ofstream attach function

Dennis,
Thanks for the response.
Some more question I have for you
1: After attaching with logDest.open, how do you close the same. Actually in my code depending on some condition I am attaching it to either STDOUT or STDERR. So once I am done with the work, I need to close the same. How would you know logDest is attached to what, and correspondingly close the same.?
=======================================
2 : My application is getting core dump, while I stopping it. The gdb is not showing much info. Below is the gdb back trace of the same.


HP gdb 5.7 for HP Itanium (32 or 64 bit) and target HP-UX 11.2x.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 5.7 (based on GDB) is covered by the
GNU General Public License. Type "show copying" to see the conditions to
change it and/or distribute copies. Type "show warranty" for warranty/support.
..
Core was generated by `SCHEDULER'.
Program terminated with signal 6, Aborted.

#0 0xc00000000029da30:0 in kill+0x30 () from /usr/lib/hpux64/libc.so.1
(gdb) bt
#0 0xc00000000029da30:0 in kill+0x30 () from /usr/lib/hpux64/libc.so.1
#1 0xc0000000001c1bf0:0 in raise+0x30 () from /usr/lib/hpux64/libc.so.1
#2 0xc00000000025f610:0 in abort+0x190 () from /usr/lib/hpux64/libc.so.1
#3 0xc0000000018c5910:0 in std::terminate()+0x50 ()
from /usr/lib/hpux64/libCsup.so.1
#4 0xc0000000018ef520:0 in __cxxTerm+0x60 () from /usr/lib/hpux64/libCsup.so.1
#5 0xc0000000002072a0:0 in __exit_handler+0xa0 ()
from /usr/lib/hpux64/libc.so.1
Dennis Handly
Acclaimed Contributor

Re: class "std::basic_ofstream attach function

>1: After attaching with logDest.open, how do you close the same.

That's the problem. It may close it more than once. I.e. you can't ever close that file. You can just flush it.

>So once I am done with the work, I need to close the same. How would you know logDest is attached to what, and correspondingly close the same?

Unfortunately there is no provision for doing that. :-(
So you need to use dup(2) to make a copy of STDOUT or STDERR and then it can be closed:

#include
int fd_file = dup(STDOUT_FILENO);
logDest.open(fd_file);

>2: My application is getting core dump, while I stopping it.

You are doing a throw out of a destructor that isn't caught, during static destruction. This is illegal, 15.5.1(1). In gdb, you need to use "catch throw" just before you exit, so you can catch it.

I'm not sure if it is due to the problem in 1) above?
sushant keerti
Advisor

Re: class "std::basic_ofstream attach function

Dennis,
1 : Our application was dumping core due to close function call. In our application we were calling close like below

logDest.close();

I commented it and we are not getting the core !.

2 : I am getting compiler error, when I try to close with 1 argument as below

error #2140: too many arguments in function call
SYSLOG::logDest.close(STDOUT_FILENO);
^

3 : Is it OK, if you donot close, if you are done with the work?.
You said
int fd_file = dup(STDOUT_FILENO);
logDest.open(fd_file);

Againg open, after dup??? ( there is no close call??)






Dennis Handly
Acclaimed Contributor

Re: class "std::basic_ofstream attach function

>I commented it and we are not getting the core!

Ok. You can do that or call dup(2).

>2: I am getting compiler error, when I try to close with 1 argument as below
SYSLOG::logDest.close(STDOUT_FILENO);

Why would you think that works? If it was that easy, I would have mentioned it. :-)

>3: Is it OK, if you do not close, if you are done with the work?

For stderr and stdout, they are never closed. But you should flush them.

>You said
>int fd_file = dup(STDOUT_FILENO);
>logDest.open(fd_file);
>Again open, after dup??? (there is no close call??)

Basically, replace that open by dup(2) then open. If you use dup(2), you can leave that close call there.
Kishan Rajagopal
New Member

Re: class "std::basic_ofstream attach function

Dennis
I and Sushant work in the same team,
The same piece of code is running fine on HP-UX 11.0, Can you please let us know if in the old versions you were ignoring the close call on STDIN and STDERR