Okay, this is really a C question, not a C++ one, but cut me a little slack
please. :)
Anyway, considering one redirects a standard stream with, say, the following
code:
FILE *fp = freopen( "test.txt", "w", stdout );
puts( "This is a test." );
How would one reset stdout such that it writes data to the console?
TIA,
Mike 3 1760
On Mon, 2 Feb 2004 11:07:31 -0800 in comp.lang.c++, "Michael W. Hicks"
<mh*****@speakeasy.net> was alleged to have written: Okay, this is really a C question, not a C++ one, but cut me a little slack please. :)
Wot, got toasted over on comp.lang.c? Unfortunately, what you are
asking is outside the standardized boundaries of either language. The
following has been known to work on various platforms and is IMO as
portable as you can get on this.
Anyway, considering one redirects a standard stream with, say, the following code:
int save_stdout = dup(fileno(stdout));
FILE *fp = freopen( "test.txt", "w", stdout ); puts( "This is a test." );
How would one reset stdout such that it writes data to the console?
dup2(save_stdout, fileno(stdout));
close(save_stdout);
"David Harmon" <so****@netcom.com> wrote in message
news:40****************@news.west.earthlink.net... Wot, got toasted over on comp.lang.c? Unfortunately, what you are asking is outside the standardized boundaries of either language. The following has been known to work on various platforms and is IMO as portable as you can get on this.
To be honest, I wasn't subscribed to any of the C newgroups but I was
subscribed to this one and figured the question wasn't totally off-topic. I
probably should have taken the time to post the question to the most
appropriate group(s). I apologize.
It's strange that the ANSI standard allows for redirecting a default stream,
but it doesn't specify a manner to reset it. That seems like an oversight.
Mike
On Mon, 2 Feb 2004 11:07:31 -0800, "Michael W. Hicks"
<mh*****@speakeasy.net> wrote in comp.lang.c++: Okay, this is really a C question, not a C++ one, but cut me a little slack please. :)
Anyway, considering one redirects a standard stream with, say, the following code:
FILE *fp = freopen( "test.txt", "w", stdout ); puts( "This is a test." );
How would one reset stdout such that it writes data to the console?
TIA, Mike
The C standard, from which C++ inherits these functions, does not
define a way to do this, nor guarantee that it is possible on all
platforms.
If your platform provides a name, representable as a character string,
then it is quite likely that another call to freopen() can do the
trick. Such names might be "con" or "/dev/tty" or some such, all very
platform specific.
The real problem is that based on some early implementations, and
perhaps on some still current and/or in use, not only the contents of
the structure pointed to by the FILE *, but also the actual address
may be significant.
This is covered in the comp.lang.c FAQ, BTW, link in my signature.
--
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++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ronald Legere |
last post by:
The new itertools stuff is pretty cool. One thing that bothers me though is
that
there seems to be no way to copy an iterator. How does one work around this?
Is
there a trick that I am missing?
...
|
by: Oz |
last post by:
This is long. Bear with me, as I will really go through all the convoluted
stuff that shows there is a problem with streams (at least when used to
redirect stdout).
The basic idea is that my...
|
by: Tron Thomas |
last post by:
What does binary mode for an ofstream object do anyway? Despite which
mode the stream uses, operator << writes numeric value as their ASCII
representation.
I read on the Internet that it is...
|
by: George B |
last post by:
When I redirect standard output, I would like to duplicate a few lines of
this output to the console. If I add duplicates of the desired source
statements, changing cout to cerr, it works fine,...
|
by: bonj |
last post by:
hello
I hope somebody can help me get my head around this area of 'stream'
programming... I know that streams are very fashionable nowadays so
hopefully there'll be lots of replies. ;-)
...
|
by: Vikrant |
last post by:
Friends,
Due an application (old) install program problem, Under AIX I could
only create DB2 instance, I could also catalog it. Its complex
application and I am not expert in creating full...
|
by: Kobu |
last post by:
I have a question about C's abstract "streams" (that I can't seem to
FULLY understand from reading several tutorials).
Streams seems to suggest that input can be treated continously if
needed....
|
by: bonk |
last post by:
Hello
how do I connect streams in c# ?
Imagine the followung scenario: I have a StreamWriter that writes Text to a
Stream. How can I tell that Stream to pass that Data to another Stream...
|
by: Chris |
last post by:
I'm reading up on streams and I have two articles that
seem to conflict with each other. One article describes
streams and lists a few of the major ones (FileStream,
Memory Stream, Network...
|
by: Ian Davies |
last post by:
Hello
I am struggling for a solution to clear some fields on my webpage that takes
their values from some sessions
My solution below works when the button is clicked twice. I sort of know why
I...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |