freopen & cout

A

Alex Vinokur

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:[email protected]
http://up.to/alexvn
=====================================
 
J

Jack Klein

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top