473,848 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

redirecting stdout to/from console

10 New Member
Hi, all.

I am coding in C on Linux, and using an external library provided by others. This library generates a batch of error/warning messages to stdout, which I do not want to see.

Before each function call to the external library, I can insert the following:
Expand|Select|Wrap|Line Numbers
  1. freopen("/dev/null", "w", stdout);
However, after the function call I cannot figure out how to reroute stdout to the console.

I tried early in the program defining:
Expand|Select|Wrap|Line Numbers
  1. FILE *fConsole = stdout;
and then following the function call with
Expand|Select|Wrap|Line Numbers
  1. freopen(fConsole, "w", stdout);
I also tried following the function call with
Expand|Select|Wrap|Line Numbers
  1. freopen("CON", "w", stdout);
Neither worked; all subsequent messages written to stdout continue to go to /dev/null; that is, they disappear.

So... how do I get the stdout messages back to the console after my external function has executed?

Thanks!
Aug 2 '07 #1
4 9756
gpraghuram
1,275 Recognized Expert Top Contributor
Hi, all.

I am coding in C on Linux, and using an external library provided by others. This library generates a batch of error/warning messages to stdout, which I do not want to see.

Before each function call to the external library, I can insert the following:
Expand|Select|Wrap|Line Numbers
  1. freopen("/dev/null", "w", stdout);
However, after the function call I cannot figure out how to reroute stdout to the console.

I tried early in the program defining:
Expand|Select|Wrap|Line Numbers
  1. FILE *fConsole = stdout;
and then following the function call with
Expand|Select|Wrap|Line Numbers
  1. freopen(fConsole, "w", stdout);
I also tried following the function call with
Expand|Select|Wrap|Line Numbers
  1. freopen("CON", "w", stdout);
Neither worked; all subsequent messages written to stdout continue to go to /dev/null; that is, they disappear.

So... how do I get the stdout messages back to the console after my external function has executed?

Thanks!
Hi,
Try this idea.
1)Open a file in write mode
2)use dup system call to swap the file descriptor to stdout
3)Open a file in write mode
4)use dup system call to swap the file descriptor to stderr
5)call ur functions/library
6)restore the file descriptor back to stdout and stderr.
7)close the file

u have to use open call instead of fopen for this.
Hope this helps u

Raghuram
Aug 2 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Maybe I'm missing something, but you can't do away with stdout, stderr and stdin.

This code:
freopen("/dev/null", "w", stdout);
does not redirect stdout. This code does:
Expand|Select|Wrap|Line Numbers
  1. FILE* ElderGeek = freopen("/dev/null", "w", stdout);
  2.  
From here on if you use ElderGeek, the data goes to the file but if you use stdout it still goes to the screen. Like:

Expand|Select|Wrap|Line Numbers
  1.  fprintf( ElderGeek, "This will go to the file\n" );
  2.  fprintf(stdout, "This still goes on the screen\n");
  3.  
Aug 2 '07 #3
@weeknessforcat s
check this out:
http://www.cplusplus.com/reference/c...stdio/freopen/

here is the code snippet they posted:
/* freopen example: redirecting stdout */
#include <stdio.h>

int main ()
{
freopen ("myfile.txt"," w",stdout);
printf ("This sentence is redirected to a file.");
fclose (stdout);
return 0;
}

This site: http://support.microsoft.com/kb/58667 says that you should use freopen("CON", "w", stdout) on windows. I don't know about on other platforms though.

Although, I have never tried it.
Oct 1 '10 #4
donbock
2,426 Recognized Expert Top Contributor
Are you sure the errors and warnings aren't going to stderr? It can be hard to tell because both stderr and stdout normally go to the console.

Why not redirect stdout (or stderr) on the command line rather than from within your program?
Oct 3 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1067
by: Tim Stanka | last post by:
Hello, I have created a Python class with a Write() function which grabs stdout from the embedded interpreter and appends onto to the text already in a CEdit multiline control with the help of a C++ extension. I was curious if there was a way to grab stdout via the C-API? Thanks, Tim
2
6135
by: Volly | last post by:
Helo ! I want write console application and I want to reuse my old class, but inside it is used Image class (and System.Drawing.Imaging namespace). I can't complile it. The error is: The type or namespace name 'Drawing' does not exist in the class or namespace 'System' (are you missing an assembly reference?)
1
1685
by: Teis Draiby | last post by:
I am working on a Win32 program with an additional console window to output messages. I also want to type commands in the console window. Is there any common way to achieve that? For example how do I output messages while the console is listning for command inputs? I'm looking forward for your answer with great anticipation
3
2967
by: Patrick Porter | last post by:
Im trying to capture the stdout from running a process from within my app. I've been dinking around using the code straight off the MS site. i can get "ipconfig / all" (for example) to send back its output, but not (in this case) "nasm -v" , which displays version information. whats happening? slightly modified MS code: Dim myProcessStartInfo As New ProcessStartInfo("c:\nasm\nasm", "-v") myProcessStartInfo.UseShellExecute = False
1
2598
by: pp4175 | last post by:
Hello Everyone, I am currently working on writing a GUI wrapper for a Menu driven DOS Program. What I was looking on doing is redirecting stdout/stdin to a stream and read/write similar to a file. My issue is that I try to read from the stdout stream and never get anything only if the program has exited. Is there a way of reading the redirected stdout from a DOS program in say real time? I have tried the async (BeginOutputReadLine) and...
2
6108
by: leoman730 | last post by:
Hi, I need some help Anyone could tell me how to run a .exe file from console?? For example, I have a file called A.exe, how can I execute it in the console (WinXP cmd) I have tried to use this: run A.exe but I could see this start the program however it was hidden such that i didnt see anything happen in the console but if i check teh window task manager, i found the process for this application. Anyway to show the exectution in the...
1
1713
by: Falcolas | last post by:
I have a rather strange situation, and I'm not sure my brief experience of Python will let me handle it properly. The situation is this: I have a Java class "X" which I need to call in a Jython script. The output of "X" is sent to stdout using the java call System.out. I need to capture this output, and preform tests on it in the Jython script. My first pass at a design involves two jython scripts. One (we'll call it "Y") whose sole...
2
1485
by: Jade Lee | last post by:
Hi. I want to read a character from console, while writing something to the console. And if, there were no input character in some seconds, then I wish to continue to writing to the console. How can I done that with .NET?
6
2948
by: drhilbert | last post by:
Dear all, in these days, we have found a problem we can't solve even after long long googling, so we are here asking your precious help. In our program instead of using cout to print messages we implemented a custom ostream able to do the following: * Allow the user to choose where to send the messages: on the stdout, in to a file or to any other ostream
1
1589
by: virendrachandak | last post by:
Hi, I have created a Windows application (in VB .NET). Now I want it to run from Console too. However, when i run it from console, i don't want to should the windows forms and also want to run some specific tasks.
0
9892
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9735
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10997
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10661
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9497
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7889
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7067
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5917
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3172
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.