Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Stream Redirection. Newbies question

Reply
Thread Tools

Stream Redirection. Newbies question

 
 
Alexander Baranov
Guest
Posts: n/a
 
      10-25-2004
Hi.
I work in Debian Linux GCC. I have a function, which opens a socket and
binds 'mystream' stream with this socket (by fdopen command). Then, while
being inside this function I want stdout be redirected to mystream. On
exiting the function I have to return stdout to ordinary stdout.
I did it like this:

Entering function
{
...
FILE* sos_stdout;
sos_stdout = stdout; //reserving stdout
stdout = mystream; //redirecting
....
doing something
....
stdout = sos_stdout; //returning stdout back
fclose(mystream); //closing my stream
.....
Exiting..
}

It works, but I still have a feeling that I do it in a wrong way. Please,
advise if it is really so.
Regards, Alex.


 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      10-25-2004
Alexander Baranov wrote:
[...]
> FILE* sos_stdout;
> sos_stdout = stdout; //reserving stdout
> stdout = mystream; //redirecting


This statement won't work on all platforms, as "stdout" may not be an
lvalue. For example, several of my systems have stdio.h lines like:

#define stdin (&__iob[0])
#define stdout (&__iob[1])
#define stderr (&__iob[2])

[...]

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
Dan Pop
Guest
Posts: n/a
 
      10-25-2004
In <81066$417cfa3c$44a55b44$(E-Mail Removed) s.com> "Alexander Baranov" <(E-Mail Removed)> writes:

>Entering function
>{
>..
>FILE* sos_stdout;
>sos_stdout = stdout; //reserving stdout
>stdout = mystream; //redirecting
>...
>doing something
>...
>stdout = sos_stdout; //returning stdout back
>fclose(mystream); //closing my stream
>....
>Exiting..
>}
>
>It works, but I still have a feeling that I do it in a wrong way. Please,
>advise if it is really so.


You're right. stdin, stdout and stderr need not be modifiable lvalues,
so you may not be allowed to assign anything to them (even if it works
on your particular implementation).

Use fprintf on mystream instead of printf (ditto for fputs instead puts)
and you don't have to mess with stdout.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
Currently looking for a job in the European Union
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How we recognize newbies as newbies Alf P. Steinbach C++ 31 08-21-2007 05:12 PM
Newbies Question on data variables Vivian ASP .Net 2 07-12-2006 10:21 AM
newbies question about return value Chinmoy Mukherjee C++ 3 06-15-2005 06:33 PM
Re: Newbies? - Usenet don't need no steenking newbies! =?iso-8859-1?Q?=B1?= Digital Photography 2 07-17-2004 02:26 PM
Question about newbies and interfaces Joona I Palaste Java 2 11-30-2003 04:24 PM



Advertisments