473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 3148
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I...
8
by: pavel.orehov | last post by:
Hi, I am using flex and bizon to write HTTP parser. I am passing well flex and bison tools but can't compile their output. ================= Flex file (http_parser.lpp) ============== %{...
2
by: Alex Vinokur | last post by:
Hi, freopen closes stdout, but it seems that fclose doesn't restore it (see sample below). How can one print to stdout after using freopen? ========= C code : File foo.c : BEGIN...
3
by: John Devereux | last post by:
Hi, I am a bit confused about the behaviour of freopen() and the standard i/o streams. I am trying to redirect a stream (stderr) to a particular "file". But, if the call fails I want it to...
4
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
3
by: rag84dec | last post by:
Hi, I have a code... int t=0x40000006,y=0x000000ff,i=0x40000000,a=0xf0000000; cout<<(t&y); cout<<"\n"; if(m1=i&&(t&y)) cout<<"Here"<<i&&(t&y);
4
by: Hlk.Turk | last post by:
Hi All, I have a question regarding the standard C library. Is it possible to have one process (program) that redirects its stdin to a file, and have a second program use that file...
2
by: Guillaume Dargaud | last post by:
Hello all, a while ago I was pointed towards freopen as a way to redirect stderr to a log file. It works great, but apparently the app also writes a few lines to stdout. Now I could redirect to 2...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.