468,771 Members | 1,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

freopen & cout

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:al****@connect.to
http://up.to/alexvn
=====================================

Jul 22 '05 #1
1 2933
On Tue, 2 Dec 2003 09:30:33 +0200, "Alex Vinokur" <al****@bigfoot.com>
wrote in comp.lang.c++:
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 ===========


Ask in a compiler/OS specific newsgroup. The language has nothing at
all to say about what happens when you try to open a file for output
more than once simultaneously. Even if you do it entirely with C++
streams or C stdio FILE pointers.

This is not a language issue.

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Steven T. Hatton | last post: by
8 posts views Thread by pavel.orehov | last post: by
2 posts views Thread by Alex Vinokur | last post: by
3 posts views Thread by John Devereux | last post: by
4 posts views Thread by alisaee | last post: by
3 posts views Thread by rag84dec | last post: by
4 posts views Thread by Hlk.Turk | last post: by
2 posts views Thread by Guillaume Dargaud | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.