public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* freopen & cout
@ 2003-12-02 18:27 Alex Vinokur
  2003-12-02 18:40 ` Burak Serdar
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Vinokur @ 2003-12-02 18:27 UTC (permalink / raw)
  To: gcc-help

Hi,

Where has output-to-cout gone in the program below?

Thanks,

========= C++ code : File foo.cpp : BEGIN =========
#include <cstdio>
#include <cassert>
#include <iostream>
#include <fstream>
using namespace std;

#define FILE_NAME "foobar"

int main ()
{
ofstream ofs;

  remove (FILE_NAME);

  ofs.open (FILE_NAME);
  assert (ofs);
  assert (ofs.is_open());

FILE* stdof = freopen (FILE_NAME, "w", stdout);
  assert (stdof);

  cout << "to cout (first time)" << endl;   // Problematic line
  ofs  << "to ofs  (first time)" << endl;
  cout << "to cout (second time)" << endl;  // Problematic line
  ofs  << "to ofs  (second time)" << endl;

  ofs.close();
  assert (!ofs.is_open());

int rc = fclose (stdof);
  assert (!rc);

  return 0;
}
========= C++ code : File foo.cpp : END ===========


========= Compilation & Run : BEGIN =========

$ g++ -v
[---omitted---]
gcc version 3.3.1 (cygming special)

$ g++ -W -Wall foo.cpp

$ a
// No output; Where has output-to-cout gone?

$ cat foobar
to ofs  (first time)
to ofs  (second time)

========= Compilation & Run : END ===========


--
   =====================================
   Alex Vinokur
     mailto:alexvn@connect.to
     http://up.to/alexvn
   =====================================









^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: freopen & cout
  2003-12-02 18:27 freopen & cout Alex Vinokur
@ 2003-12-02 18:40 ` Burak Serdar
  0 siblings, 0 replies; 3+ messages in thread
From: Burak Serdar @ 2003-12-02 18:40 UTC (permalink / raw)
  To: Alex Vinokur; +Cc: gcc-help

Alex Vinokur wrote:
> Hi,
> 
> Where has output-to-cout gone in the program below?
> 
> Thanks,
> 
> ========= C++ code : File foo.cpp : BEGIN =========
> #include <cstdio>
> #include <cassert>
> #include <iostream>
> #include <fstream>
> using namespace std;
> 
> #define FILE_NAME "foobar"
> 
> int main ()
> {
> ofstream ofs;
> 
>   remove (FILE_NAME);
> 
>   ofs.open (FILE_NAME);
>   assert (ofs);
>   assert (ofs.is_open());
> 
> FILE* stdof = freopen (FILE_NAME, "w", stdout);

This call to freopen closes stdout, therefore you cannot print anything 
using cout after this point.



>   assert (stdof);
> 
>   cout << "to cout (first time)" << endl;   // Problematic line
>   ofs  << "to ofs  (first time)" << endl;
>   cout << "to cout (second time)" << endl;  // Problematic line
>   ofs  << "to ofs  (second time)" << endl;
> 
>   ofs.close();
>   assert (!ofs.is_open());
> 
> int rc = fclose (stdof);
>   assert (!rc);
> 
>   return 0;
> }
> ========= C++ code : File foo.cpp : END ===========
> 
> 
> ========= Compilation & Run : BEGIN =========
> 
> $ g++ -v
> [---omitted---]
> gcc version 3.3.1 (cygming special)
> 
> $ g++ -W -Wall foo.cpp
> 
> $ a
> // No output; Where has output-to-cout gone?
> 
> $ cat foobar
> to ofs  (first time)
> to ofs  (second time)
> 
> ========= Compilation & Run : END ===========
> 
> 
> --
>    =====================================
>    Alex Vinokur
>      mailto:alexvn@connect.to
>      http://up.to/alexvn
>    =====================================
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: freopen & cout
@ 2003-12-02 18:48 lrtaylor
  0 siblings, 0 replies; 3+ messages in thread
From: lrtaylor @ 2003-12-02 18:48 UTC (permalink / raw)
  To: alexvn, gcc-help

Alex,

According to MSDN:

"The freopen function closes the file currently associated with stream
and reassigns stream to the file specified by path."

That probably means that the original stdout (to which cout writes) has
been closed, and so your cout output is getting lost in the big void...
I know that you were probably expecting cout to get redirected to the
file, but perhaps this just isn't the right way to do it (due to how
cout is implemented in C++, etc.)...

Good luck,
Lyle

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Alex Vinokur

Hi,

Where has output-to-cout gone in the program below?

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-12-02 18:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-02 18:27 freopen & cout Alex Vinokur
2003-12-02 18:40 ` Burak Serdar
2003-12-02 18:48 lrtaylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).