This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
ofstream problems
- From: Liam Routt <caligari at cs dot mu dot oz dot au>
- To: gcc-help at gcc dot gnu dot org
- Cc: Cameron Blackwood <korg at cs dot mu dot oz dot au>
- Date: Mon, 6 May 2002 18:04:27 +1000
- Subject: ofstream problems
I have recently installed a Mandrake 8.2 linux system and the g++ 3.0.4
that is offered with the distribution. Previously I've been using 2.96.x
on linux and a host of other platforms.
I've encountered the differences with ofstreams, where I cannot create and
ofstream with an existing file descriptor [e.g. ofstream useOld(oldFD) ].
I can live with that.
In trying to rewrite some of our existing code to do things in a more
compatible way, I've found a place where I was using an open() to get a FD
and then using that to create an ofstream. There is no other use of the
FD, so I should be able to create the ofstream directly instead. But I'm
having a lot of trouble with the construtors for ofstream (and the rather
confusing - to me - errors my attempts are generating).
In fstream.h I find:
======
class ofstream : public fstreambase, public ostream {
public:
ofstream() : fstreambase() { }
ofstream(int fd) : fstreambase(fd) { }
ofstream(int fd, char *p, int l) : fstreambase(fd, p, l) { } /*Deprecated*/
ofstream(const char *name, int mode=ios::out, int prot=0664)
: fstreambase(name, mode | ios::out, prot) { }
void open(const char *name, int mode=ios::out, int prot=0664)
{ fstreambase::open(name, mode | ios::out, prot); }
};
======
Which says to me that I should be able to create an ofstream from a
filename, a mode value, and a permissions value (constructor 4, or even
the open method), not at all unlike what I was using for the call to open().
So I have the following code:
======
#include <iostream.h>
#include <fstream.h>
const char *outFile = "filetest";
int main ( )
{
ostream* errorFile = NULL;
#ifdef FAILS
errorFile = new ofstream(outFile, ios::out | ios::in | ios::trunc, 0640);
#else
errorFile = new ofstream(outFile, ios::out | ios::in | ios::trunc);
#endif
*errorFile << "This is output" << endl;
return ( 0 );
}
======
This is fine as written, and allows me to create a file and write to it.
But if I #define FAIL, thus asking for the full constructor with the
permissions info as well, I get the following error on compile:
======
test.cc: In function `int main()':
test.cc:16: no matching function for call to `std::basic_ofstream<char,
std::char_traits<char> >::basic_ofstream(const char*&,
std::_Ios_Openmode,
int)'
/usr/include/g++-v3/bits/std_iosfwd.h:84: candidates are:
std::basic_ofstream<char, std::char_traits<char>
>::basic_ofstream(const
std::basic_ofstream<char, std::char_traits<char> >&)
/usr/include/g++-v3/bits/std_fstream.h:319:
std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*,
std::_Ios_Openmode = (std::ios_base::out | std::ios_base::trunc)) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/include/g++-v3/bits/std_fstream.h:313:
std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT =
char,
_Traits = std::char_traits<char>]
======
Which seems to be claiming that there is no constructor with the three
arguments that I need. I get similar sorts of messages when I create an
ofstream with the default constructor and try to use the open() method.
Anyone have any solid suggestions for a) why what I'm trying is not
working, and b) what I can do to get the same results. It shouldn't be
this hard to create a file using ofstreams.
Take care,
Liam
--
Liam Routt liam@routt.net
Darcsyde Productions http://www.routt.net/Caligari/
-- still waiting for the Absolute Destiny Apocalypse --