473,598 Members | 3,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MinGW: How do I temporarily disable stderr in a C(++) program

Hi all.

I've installed Bloodshed Dev-C++ on a Windows 2000 SP4 machine. I'm using
MinGW 3.4.2.

I'd like to temporarily disable standard functions to write to stderr, i.e.
for instance redirect stderr to a temporary file (or /dev/null but is there
an equivalent under Windows? Is it "nul:") and then to *restore* the
default stderr so that standard library functions that write to stderr
produce output again.

I've checked freopen to redirect stderr to a file but, strangely enough,
error messages don't go to the file :(.

Also note I'm using stdio and iostream concurrently.

Is there a way to, say, close stderr and restore it later on in the same
program under Windows using standard libraries?

Thanks for any hint or suggestion.

Vince C.
Jul 14 '07 #1
37 5024
In article <46************ ***********@rea d.news.be.uu.ne t>,
Vince C. <no**@teledisne t.bewrote:
>Hi all.

I've installed Bloodshed Dev-C++ on a Windows 2000 SP4 machine. I'm using
MinGW 3.4.2.

I'd like to temporarily disable standard functions to write to stderr, i.e.
for instance redirect stderr to a temporary file (or /dev/null but is there
an equivalent under Windows? Is it "nul:") and then to *restore* the
default stderr so that standard library functions that write to stderr
produce output again.
Let me be the first of many to wish you all the best of luck and true
happiness, and, oh, yes, to point out that your question is:

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.
--
Useful clc-related links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language

Jul 14 '07 #2
Vince C. <no**@teledisne t.bewrote:
I've installed Bloodshed Dev-C++ on a Windows 2000 SP4 machine. I'm using
MinGW 3.4.2.
I'd like to temporarily disable standard functions to write to stderr, i.e.
Can you name a single standard C function that writes to stderr
(of course except those output functions that you tell to write
to stderr - and for those it's already under your control where
they write to)?
for instance redirect stderr to a temporary file (or /dev/null but is there
an equivalent under Windows? Is it "nul:") and then to *restore* the
default stderr so that standard library functions that write to stderr
produce output again.
If you use freopen() to redirect stderr to a file then stderr gets
closed. If you somehow can get it back despite having it closed and
then re-redirect back to that again is a question that you have to
ask in a Windows group.
I've checked freopen to redirect stderr to a file but, strangely enough,
error messages don't go to the file :(.
What exactly did you do? Perhaps it's a buffering issue, i.e.
while stderr is line buffered the writes to new file are block-
buffered and, if your program crashes, the buffer isn't written
to the file?
Also note I'm using stdio and iostream concurrently.
There's no 'iostream' in C, I guess you must be using C++, not C,
and then you should ask in comp.lang.c++.
Is there a way to, say, close stderr and restore it later on in the same
program under Windows using standard libraries?
You would need to have a file name for stderr to be able to that
since the only function for redirection in standard C is freopen()
and this can only redirect to a file given by path, not to a FILE
pointer. If Windows supplies you with something in the file system
for (an already closed) stderr I can't tell, that you again need to
ask in a Windows group. But this solution then is already system
specific for this reason you probably also don't need to care if
the functions you use are standard C functions...

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jul 14 '07 #3
Jens Thoms Toerring wrote:
Vince C. <no**@teledisne t.bewrote:
I've installed Bloodshed Dev-C++ on a Windows 2000 SP4 machine. I'm
using MinGW 3.4.2.
I'd like to temporarily disable standard functions to write to
stderr, i.e.

Can you name a single standard C function that writes to stderr
(of course except those output functions that you tell to write
to stderr - and for those it's already under your control where
they write to)?
perror().


Brian
Jul 14 '07 #4
Default User <de***********@ yahoo.comwrote:
Jens Thoms Toerring wrote:
Vince C. <no**@teledisne t.bewrote:
I've installed Bloodshed Dev-C++ on a Windows 2000 SP4 machine. I'm
using MinGW 3.4.2.
I'd like to temporarily disable standard functions to write to
stderr, i.e.
Can you name a single standard C function that writes to stderr
(of course except those output functions that you tell to write
to stderr - and for those it's already under your control where
they write to)?
perror().
Got me;-) And assert() is another example that I didn't think of...

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jul 14 '07 #5
Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

Warning: troll behind!
Let me answer with the same terseness:
Off-topic
No! I'm using all but Windows API, only standard C function alls and GPL'd
code so that it runs on BOTH Win* and LINUX systems.
Not portable
Yes! I'm using all portable APIs like GNU getopt() and getopt_long().
Absolutely no Windows API is involved.
Can't discuss it here
Ah? So stderr is no part of C?

I suggest you read once more the C standard functions.

Thanks for your non-help anyways,
Vince C.
Jul 14 '07 #6
Tim Prince wrote:
This isn't a Windows help forum.
I'm not that dumb you know... I just posted enough information so that those
who want to help get the most accurate picture of what I'm trying to do and
on what platform.

Vince C.
Jul 14 '07 #7
Vince C. wrote:
>Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

Warning: troll behind!
On occasion Kenny'll provide useful answers, but his troll messages are best
ignored. That said, I disagree with your reply to him.
Let me answer with the same terseness:
>Off-topic

No! I'm using all but Windows API, only standard C function alls and GPL'd
code so that it runs on BOTH Win* and LINUX systems.
The license of code has nothing whatsoever to do with its portability, and I
fail to see how anyone might disagree. Plus, there are many more systems
than Windows and Linux, multiple processors that they run on, multiple
implementations of the C standard library per operating system, multiple
compilers per operating system, and there are probably other things I left
out.
>Not portable

Yes! I'm using all portable APIs like GNU getopt() and getopt_long().
Absolutely no Windows API is involved.
GNU getopt and getopt_long are not standard C functions. If you're calling
them externally, that's not portable, and it's off-topic here. If you're
including them in your own project, and they're doing something you don't
want, you'll need to show the relevant code.
Jul 14 '07 #8

"Jens Thoms Toerring" <jt@toerring.de wrote in message
news:5f******** *****@mid.uni-berlin.de...
Vince C. <no**@teledisne t.bewrote:
>I've installed Bloodshed Dev-C++ on a Windows 2000 SP4 machine. I'm using
MinGW 3.4.2.
>I'd like to temporarily disable standard functions to write to stderr,
i.e.

Can you name a single standard C function that writes to stderr
(of course except those output functions that you tell to write
to stderr - and for those it's already under your control where
they write to)?
assert() will often do so, though it is not obliged to.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jul 14 '07 #9
Jens Thoms Toerring wrote:
>I'd like to temporarily disable standard functions to write to stderr,
i.e.

Can you name a single standard C function that writes to stderr
(of course except those output functions that you tell to write
to stderr - and for those it's already under your control where
they write to)?
Functions getopt() and getopt_long(). They write error messages to stderr
whenever an option is not recognized or there is something wrong with the
command line syntax defined by the application.

If you use freopen() to redirect stderr to a file then stderr gets
closed. If you somehow can get it back despite having it closed and
then re-redirect back to that again is a question that you have to
ask in a Windows group.
It should be basically as simple as initializing device stderr when the
program starts, shouldn't it? There is code within stdlib that creates an
__iob[2] array of FILE*, each being stdout, stdin and stderr, respectively.
If my programm succeeds in doing the same, i.e. re-initializing stderr with
the default values, the trick is done. So I asked the question if it was
possible.

Of course my program is (currently) a Windows application but - while I'm
writing it - I'm building a set of functions that I'll be able to reuse
under whatever platform I want, provided it's running GNU code.

>I've checked freopen to redirect stderr to a file but, strangely enough,
error messages don't go to the file :(.

What exactly did you do? Perhaps it's a buffering issue, i.e.
while stderr is line buffered the writes to new file are block-
buffered and, if your program crashes, the buffer isn't written
to the file?
I'm trying to create a library of functions that basically runs with C++
code but with a base set of C functions. That base set of C functions
should be able to handle multiple command line argument syntaxes, like
argtable2 but much simpler (for my own usage).

I've read argtable2 uses get_opt() and getopt_long() so is 100% compatible
with GNU getopt() syntax. However if you want multiple command line
syntaxes and are using only getopt() and getopt_long() both functions
output error messages with the syntaxes that don't match - since there
should be only one command line parameter syntax that matches but you have
to check them all in a while loop (for instance) until you find one that
matches, i.e. during which neither getopt() and getopt_long() return any
error message.

Hence I'd like to temporarily disable stderr while I'm checking command line
parameter syntaxes since I might get error messages, which is the normal
behaviour of these functions.

>Also note I'm using stdio and iostream concurrently.

There's no 'iostream' in C, I guess you must be using C++, not C,
and then you should ask in comp.lang.c++.
Yes, I know there is no such iostream in C but iostream uses std* file
descriptors. Hence I suppose I must keep both libraries in sync, i.e. if I
close (or reopen) stderr cerr must be kept in sync so that it "knows" the
stderr file descriptor has changed.

I posted here for it's a C question first. And given the length of this
post, you can easily understand it's a hassle to type it again... ;-)

>Is there a way to, say, close stderr and restore it later on in the same
program under Windows using standard libraries?

You would need to have a file name for stderr to be able to that
since the only function for redirection in standard C is freopen()
and this can only redirect to a file given by path, not to a FILE
pointer. If Windows supplies you with something in the file system
for (an already closed) stderr I can't tell, that you again need to
ask in a Windows group. But this solution then is already system
specific for this reason you probably also don't need to care if
the functions you use are standard C functions...
I'd say "no" since the libraries I'm using implement the same code on all
supported platforms (it's GNU code). So it's completely independent from
Windows. The only difference is there is no /dev/null, as when we redirect
output in shell scripts. I think that device is "nul:" under windows (I
remember batch scripts where I used:

do_something_wi th_possible_err or_messages nul:

The only thing I need to know, I suppose, is how does standard C library
initialize stderr so that I can do the same in my program - provided it's
safe, of course. Hence my question(s)...

Vince C.
Jul 14 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6229
by: Rafal 'Raf256' Maj | last post by:
Hi, is this group good for questions also little compiler/platform related? int tab; program with this instruction compiled undre MinGW(DevCPP) and executed under WinXP - crashes in this instruction. Probably due to stack overflow error. How can I increase stack limit? Is there some universal method for most C/C++ compilers ?
66
5455
by: Srijit Kumar Bhadra | last post by:
Is there any specific reason for not using MinGW to build the official distribution of Python for Win32? A quick Google search did not reveal the answer to my question. If a link is available, please post it. Best Regards, Srijit
4
6627
by: Jon Slaughter | last post by:
Is there any method to temporarily disable focus changing?(I assume only method is tab or mouse?) This problem has been tieing me up for a while and nothing seems to work. The only thing that I *know* will work is to disable all tab stops on all controls. This brute force method doesn't seem like a good idea though. What I wanted to do was take control the control enter/leave and mouse enter/leave and "switch back" the focus when it is...
3
5059
hsriat
by: hsriat | last post by:
How can I temporarily disable vertical scrollbar? I have replaced confirmation boxes and alert boxes with inlay popup DIVs. But while they are on the screen, I need to disable scrollbar of the page so that user may not access the main page (just like in conformation boxes). Also I need to disable the main page leaving that popup DIV. Scrollbar is the main issue. How can I do it?
5
8960
by: Joakim Hove | last post by:
Hello, I have written a program in C; this programs uses an external proprietary library. When calling a certain function in the external library, the particular function writes a message to stdout. I am not particularly interested in this message, and would like to silence it - however I do not know how to do it. (I stdout and stderr my self, so just redirecting into oblivion is not an option). An excerpt of the code looks like this:
10
4427
by: Isaac Gouy | last post by:
$ ./test_fail 1 Floating point exception - but this leaves 'somefile' zero size $ ./test_fail 1>somefile Floating point exception
1
6686
by: Lincoln Yeoh | last post by:
Hi, I've just started to learn python (I've been using perl for some years). How do I redirect ALL stderr stuff to syslog, even stderr from external programs that don't explicitly change their own stderr? Say I have a program called foo: #!/usr/bin/python
1
4345
by: LoneWolf | last post by:
Hi im trying to compile some code im developing that uses opengl and glut on mingw(using eclipse as the IDE) but im having linker errors like: g++ -o(OUTPUT).exe (.o files here) -lopengl32 -lglu32 -lglaux -lglut32 Warning: resolving __imp__glEnable by linking to __imp__glEnable@4 Use --enable-stdcall-fixup to disable these warnings Use --disable-stdcall-fixup to disable these fixups Warning: resolving __imp__glBlendFunc by linking to...
3
6009
by: Tarzan2001 | last post by:
Hi, I've recently purchased an external hard drive case and a 160 GB hard drive to use for back-up and data transfer. I've made 3 partitions using the Computer Management program in Windows XP. I was wondering if there is any way to temporarily disable a partition so that it doesn't get infected with a virus because I want to use my external HDD to transfer data from my friends' computers, but here in the dorms not everyone is as diligent...
0
7985
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
7896
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
8389
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...
1
8048
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5438
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
3896
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3939
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1503
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1246
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.