cout, cerr and I/O redirection

R

Rick N. Backer

Hello folks,

Novice type question here.

I'm working with a very simple app here that takes a series of strings
from the console. Once the end user has finished inputting the
strings they are individually processed and the result displayed back
to the console.

The problem I am having occurs during I/O redirection. Here is where
I am at the moment.

1) Run program with keyboard input and output redirected to file.
Never saw the prompt for the end user as it went straight to the
file. Fixed this, or so I thought, by having the end user prompt
displayed using cerr. Works like a charm, until we go to test
number 2.

2) Run program with both input and output being redirected. My user
prompt prints to the console before every line it accepts from the
input file. There are no problems with the output to the second
file from this operation. As my test suite consisted of ten
strings just to keep this simple one can see where, on large
files this behaviour would be annoying at the least but a big
performance hit no matter. I thought of using argc and argv but
when I/O redirection is in play it appears the argument count is
always 1 (program name) so is useless to me at this point.

My questions are:

Is the solution in problem 1 a reasonable approach to problem 1?

Given 1 works, how do I resolve the dilemma I now have at 2?


Thanks very much in advance.

Ken

Ken Wilson

Amer. Dlx. Tele, Gary Moore LP, LP DC Classic w/P90s,
Jeff Beck Strat, Morgan OM Acoustic,
Rick 360/12, Std. Strat (MIM), Mesa 100 Nomad,
Mesa F-30

"Goodnight Austin, Texas, wherever you are."
 
H

Heinz Ozwirk

Rick N. Backer said:
Hello folks,

Novice type question here.

I'm working with a very simple app here that takes a series of strings
from the console. Once the end user has finished inputting the
strings they are individually processed and the result displayed back
to the console.

The problem I am having occurs during I/O redirection. Here is where
I am at the moment.

I/O redirection is something your OS does to you. C++ does not know or care
about such features. But there might be some function in your compiler's
library telling you whether input and/or output have been redirected. I have
seen isatty or _isatty in several libraries, so that's what I would look
for, first.

HTH
Heinz
 
K

Ken Wilson

Heinz said:
I/O redirection is something your OS does to you. C++ does not know or care
about such features. But there might be some function in your compiler's
library telling you whether input and/or output have been redirected. I have
seen isatty or _isatty in several libraries, so that's what I would look
for, first.

Thank you for this. It was indeed the other half of the answer that I
was missing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top