By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

freopen & cout

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.